Managing multiple Python environments using Anaconda

Because members of the lab are working with different versions of Python (e.g., 2.7 vs. 3.4), I thought I’d run through a quick and easy way to manage multiple Python environments on a single computer.  There are a number of options for doing this, but I recommend using Anaconda because it is very user-friendly.

Anaconda First of all, if you aren’t using Anaconda, you should check it out.  It’s a free, easy-to-use package and environment manager for Python.  You can download it here.  It, by default, installs to your home directory, allowing you to easily work with and manage Python in environments where you don’t have root privileges.  It also automatically installs over 100 packages, and allows you to install many more using the simple command:

conda install <packagename>

If you want to install a package that Anaconda doesn't handle, you can also use pip. The version of pip (simply another package manager for Python) that comes with the Anaconda distribution will install to Anaconda's directories, thereby making the package available in any of your Anaconda environments.  You can check the version of pip you’re using with the command:

which pip

If it says something like "~/anaconda/bin/pip", where ~ is your home directory, then you can install packages with the command:

pip install <packagename>

Creating and managing environments with Anaconda I'm going to assume you’ve already downloaded and installed the Python 2.7 version of Anaconda and you want to create an environment for Python 3.4.(this will work with any other version as well, just swap out the version in the commands below).  You can download Anaconda and find installation instructions here.

If you allowed Anaconda to install to your home directory and add itself to your PATH, typing the following command in your shell or terminal should point to the Anaconda distribution in your home directory:

which conda

(for me, this results in: /Users/thw/anaconda/bin/conda)

If this worked, then conda is in your PATH and you can simply enter the command to set up a new Python environment:

conda create -n python34 python=3.4 anaconda

where “-n python34” indicates the name you want to give the environment.  It can be anything you want: “-n boa_constrictor”, “-n python3.4”, etc.  I recommend something similar to python34, which is both easy to remember and informative. The "python=3.4" part indicates the version of Python you want to use in your environment, other examples might be "python=2.7" or "python=3.3"

Then, to activate the new environment, simply use the command:

(Linux, Mac OS X) source activate python34

(Windows) activate python34

You can easily deactivate the environment and revert to your root Python by typing

(Linux, Mac OS X) source deactivate

(Windows) I don't have a Windows computer to test this on, but according to Anaconda's documentation, try: deactivate python34

This, of course, is only scratching the surface of what you can do with Anaconda.  Be sure to check out the documentation for more information.