I downloaded Anaconda and started using it on my Mac but now I am switching laptops. I will be using a Windows laptop now and I need to transfer my environments to my new laptop. How best can I do this?
I am using Python version 3.8 and was using Jupyter notebooks to run my code. But if I simply try to run the notebook on my Windows laptop I am getting one error after another (because I don't have the packages installed). Installing them one by one will take time and I don't even remember most of what I installed.
If you are working across platforms (osx-64 -> win-64) you'll need to be minimal about what packages you export from the existing environment. While Conda does have a recommended intra-platform procedure for exactly recreating environments, it does not directly translate to the cross-platform situation. Instead, try using:
conda env export --from-history > environment.yml
and then, on the new computer,
conda env create -f environment.yml
This will only export the packages that you have explicitly specified to be in the environment at some point (e.g., using conda install foo). Dependencies will be resolved automatically on the new system. This does not guarantee there still won't be packages that aren't available on Windows, but they should be less frequent and easier to resolve manually (typically by removing them from the YAML or adjusting versions).
Related
I downloaded Anaconda and started using it on my Mac but now I am switching laptops. I will be using a Windows laptop now and I need to transfer my environments to my new laptop. How best can I do this?
I am using Python version 3.8 and was using Jupyter notebooks to run my code. But if I simply try to run the notebook on my Windows laptop I am getting one error after another (because I don't have the packages installed). Installing them one by one will take time and I don't even remember most of what I installed.
If you are working across platforms (osx-64 -> win-64) you'll need to be minimal about what packages you export from the existing environment. While Conda does have a recommended intra-platform procedure for exactly recreating environments, it does not directly translate to the cross-platform situation. Instead, try using:
conda env export --from-history > environment.yml
and then, on the new computer,
conda env create -f environment.yml
This will only export the packages that you have explicitly specified to be in the environment at some point (e.g., using conda install foo). Dependencies will be resolved automatically on the new system. This does not guarantee there still won't be packages that aren't available on Windows, but they should be less frequent and easier to resolve manually (typically by removing them from the YAML or adjusting versions).
I am going through the painful process of learning how to manage packages/ different (virtual) environments in Python/Anaconda. I was told that Anaconda is basically a python installation with all the packages I need (e.g. numpy, scipy, sci-kit learn etc).
However, when I create a new environment, none of these packages is readily available. I cannot import them when using PyCharm with the newly created environment. When I check the Pycharm project interpreter, or the anaconda navigator environments tab, It seems that indeed none of these packages are installed in my new environments. Why is this? It doesn't make sense to me to provide all these packages, but then not make them ready for use when creating new environments. Do I have to install all these packages manually in new env's or am I missing something?
Kindest regards, and thanks in advance.
The reason the default python environment doesn't come with numpy is because maybe you don't want numpy in the environment. Imagine writing an API (or general software package) where your users may or may not have access to numpy. You might want to run tests to make sure your software fails gracefully or has a pure python fallback if numpy is not installed on your user's machine. Conda environments provide this (insanely useful) benefit. Of course, the package in question doesn't have to be numpy. There are some more esoteric packages where this type of testing is useful.
Furthermore, you can create a conda environment with numpy pre-installed, or any other package you want pre-installed (just add them to the end of the conda create command):
conda create --name my-env-name numpy
Anaconda comes with available packages such as numpy, scipy, and sci-kit learn, but if you want to use them within your environment, you must:
1) Create the environment:
conda create --name new_env
2) Activate the environment:
source activate new_env
3) Install the desired package using conda install
conda install numpy
If you'd like to create a new environment that includes installations of all available Anaconda packages, see create anaconda python environment with all packages. You can include anaconda in the list of packages to install in the environment, which is a 'meta-package' meaning 'all the packages that come with the Anaconda installation'.
I don't know about "conda" environments but in general virtual environments are used to provide you a "unique" environment. This might include different packages, different environment variables etc.
The whole point of making a new virtual environment is to have a separate place where you can install all the binaries ( and other resources ) required for your project. If you have some pre-installed binaries in the environment, doesn't it defeat the purpose of creating one in the first place?
The fact that you can create multiple environments helps you to separate binaries that might be needed by one and not by the other.
For instance, if you are creating a project which requires numpy:1.1 but you have numpy:2.1 installed , then you have to change it. So basically, by not installing any other packages, they are not making assumptions about your project's requirements.
You can check the packages you have in your environment with the command:
conda list
If packages are not listed you just have to add it, with the command:
conda install numpy
I am a new user to conda environment and was setting up to use TensorFlow , on Windows.
I came across a command -
source activate IntroToTensorFlow.
I understood IntroToTensorFlow is an environment we are creating, but does it mean we need to create this environment every time?. I am using jupyter notebook, so if I shutdown the kernel will the environment get deactivated?
And if I restart my PC, should I activate the environment everytime ?
Conda is a package manager that installs and manages (usually) Python libraries and (sometimes) non-Python packages. A conda environment is a sort of virtualenv virtual environment; its typical use case is to have a Python interpreter (any version) along with your choice of compatible Python libraries (any version).
The following example might most likely pertain to you. Suppose you have downloaded the implementation of a very nice paper implemented in TF and you want to try it out. But the authors implemented that when Tensorflow was just growing. The APIs have changed now, and so is the required CUDA version. You want to work ideally on the latest TF. Now, what do you do? An easy way to just try out this implementation is to create a different conda environment with the libraries needed for that implementation, run that in this environment, and perhaps if you like it, you might consider upgrading the TF APIs and use it in your code.
The conda environments are also pretty simple in its construction. If you installed conda using Anaconda and default options, you will have your environments in ~/anaconda3/envs. The environments are nothing but directories here, each having various configurations of Python interpreter and libraries of your choice. (So when you shutdown your PC/Jupyter, the environments will of course persist.) At the time of usage, you just switch between the environments to suit your needs. That is, when you source activate an environment, you will be allowed to use the Python interpreter and installed libraries from that environment. Note if you source deactivate or start a new terminal session, you will still be using the root environment.
Besides, Jupyter notebook, if setup with this plugin, will allow you to have nice integration with conda environments and you wouldn't even need to source activate everytime you want to switch. You can choose between the various settings (or conda environments), which are interpreted as different kernels in the notebook. So it would as simple as choosing some environment using a drop-down.
source activate IntroToTensorFlow does not create an environment, it simply activates an environment that has already been created. To create that environment (with tensorflow installed), use conda create -n IntroToTensorFlow tensorflow.
You do not need to create the environment every time, but you do need to activate it every time in order to use the packages installed in it. This is done using source activate IntroToTensorFlow
If you shutdown a kernel, the environment does not get deactivated automatically. To do so, you have to explicitly say source deactivate, or activate a separate environment using source activate xxx, replacing xxx with whatever environment name you want (that you have created previously).
When restarting your PC, (or starting a new session at the command line), you have to manually activate your desired environment to use it. Otherwise, by default, it will be running in your root environment. So, if you've only installed tensorflow in IntroToTensorFlow environment, you have to use source activate IntroToTensorFlow every time in order to use it.
Take a look here for more info
I've installed the official MATLAB Engine by following the instructions from the answer to Anaconda install Matlab Engine on Linux to an Anaconda virtual environment running Python3.5. I can now import matlab and matlab.engine without receiving errors. However, when I try:
matlab.engine.start_matlab(), I get 'Segmentation fault (core dumped)'
I've tried setting LD_LIBRARY_PATH from within the conda environment (in case that is even relevant): export LD_LIBRARY_PATH=/System/Library/Frameworks/Python.framework/Versions/Current/lib:$LD_LIBRARY_PATH, but to no avail. The path doesn't exist either as far as I'm aware, so I've also tried export DYLD_LIBRARY_PATH=path_to_anaconda3/envs/myEnv/lib:$LD_LIBRARY_PATH
So how can I start the matlab engine/call Matlab scripts from Python from within a Anaconda virtual environment?
I'm on Ubuntu, by the way
Short answer: there were two problems that needed to be fixed
$LD_LIBRARY_PATH should not contain a path to the Anaconda installation. Adding such a path is discouraged according to the conda documentation: https://conda.io/docs/building/shared-libraries.html, but some packages may do so anyways, causing the segmentation error.
A symbolic link is needed from a libpythonXXX.dylib file of the right version to /usr/lib/, so that MATLAB can find the right Python
Long answer: complete installation instructions for using MATLAB Engine with Anaconda
Install a MATLAB version that supports the Python you want to use. Ensure that this particular MATLAB installation is activated
Open a terminal and go to the folder containing the Python engine of the MATLAB installation: cd "/usr/local/MATLAB/R2017a/extern/engines/python"
Run setup.py with the Python version you want to use, and prefix the Anaconda environment location: sudo python3.5 setup.py install --prefix="/your_path_to_anaconda3/envs/your_env". At this point you should be able to import matlab and matlab.engine from within the Python of your Anaconda environment, but, in my case, starting the engine gave the segmentation error.
Find the libpython file of the right version. Your Anaconda environment should contain it: find /your_path_to_anaconda3/envs/your_env/ -name libpython*. In my case this returned:
/.../lib/libpython3.so
/.../lib/python3.5/config-3.5m/libpython3.5m.a
/.../lib/libpython3.5m.so.1.0
/.../lib/libpython3.5m.so
As I wanted to use it with python 3.5, I went with libpython3.5m (I don't know why the 'm' is there). Make a symbolic link from the .dylib version of this file to /usr/lib: sudo ln -s /your_path_to_anaconda3/envs/your_env/lib/libpython3.5m.dylib /usr/lib. Note that you can only have one link called libpython3.5m.dylib in /usr/lib. So if you have multiple Anaconda environments using the same version of Python, you only need to set up this link once to whichever one. Remember not to delete this environment though, as that would break the link for all other environments relying on it.
Start a new terminal (!) and activate your Anaconda environment: source activate your_env. Check within your Anaconda environment whether LD_LIBRARY_PATH contains any references to the Anaconda environment echo $LD_LIBRARY_PATH. If so, ensure that it no longer does: export LD_LIBRARY_PATH=only_paths_you_do_want_to_keep_separated_by_a_colon. This export action needs to be repeated whenever you activate your Anaconda environment, so you may want to look into more permanent means of setting it. However, in my case (apart from the fact I had been adding it myself in the hope this would improve things) the path was actually added by pygpu, so I ended up resetting LD_LIBRARY_PATH from my python script (so far without noticing ill effects).
I have Python 2.7.10 (which I just installed) and I recently installed Jupyter. However the only kind of notebook I can create when I open Jupyter is iTorch and Python 3.
How can I create a notebook in Python 2.7?
I'm attempting to complete a TensorFlow tutorial and that is written in Python 2.7.
The best way to run multiple versions of Python is with virtual environments. Then you can run whatever version you like whenever you like. A great way to do this for people who don't want to spend a lot of time working on it (like me) is with Continuum's Anaconda distribution of Python. I couldn't get the hang of virtual environments till this came along.
Before going any further, I recommend uninstalling the Python 2.7 you installed, and reverting to your system's Python as far as possible.
Then install Anaconda. Install the Python 3.5 version since you'll want to use that most of the time. Almost all libraries I use support it now. Google App Engine and TensorFlow are the only things I use that don't.
Now you should be able to create a Python 2.7 environment like so:
conda create -n python2 python=2.7 anaconda
The python2 is just the name; call it whatever you like. The anaconda bit installs the scientific Python stack; you can leave that off if you want a very basic environment.
Creating the environment doesn't start it. Start the environment with:
source activate python2
Add it to the notebook with:
ipython kernelspec install-self --user
Now install TensorFlow using whichever command Google recommends for your system.
Whenever you want to use that environment, do the source activate command again. To leave it, start another environment or type source deactivate. You can make as many environments as you like, with whatever versions of Python and whatever other packages you want.
Read more about managing environments.