I’ve been using Continuum’s enterprise Python distribution package, Anaconda, for several months now and I love it. Recently, people have been asking about Python 2.7 vs Python 3.x and so I looked into how to switch between these environments using Anaconda.

In fact, it’s quite straightforward and painless with Anaconda.

To set up a new environment with Python 3.4:

conda create -n py34 python=3.4 anaconda



Here, “py34” is a reference tag for later and “python=3.4 anaconda” are package specifications, which, according to the documentation, is the job of the SAT solver inside conda to find a consistent set of packages which satisfies these requirements. The above code will download all the new interpreter and all of the necessary dependencies to a separate directory.

To use (“activate”) the newly created environment:

source activate py34



Notice that we activate the reference tag “py34” that we chose above. Also, note that this new directory will be prepended to your path and the root Python directory will be removed (but Anaconda keeps track of all of this for you)

bash-3.2$ python
Python 3.4.3 |Anaconda 2.3.0 (x86_64)| (default, Mar  6 2015, 12:07:41) 



And to stop using (deactivate) the environment:

source deactivate  # or, in older versions of envs use `source deactivate py34`



It’s that easy! When you call Python now, it should revert back to your root environment.

bash-3.2$ python
Python 2.7.10 |Anaconda 2.2.0 (x86_64)| (default, May 28 2015, 17:04:42) 



Obviously, you can remove or delete an environment at any time by doing:

bash-3.2$ conda remove --name py34 --all



Finally, to check what environments you have installed:

conda env list



For more details, find the full set of features in the conda docs.


Published

Jul 9, 2015