Anaconda Python Environments

By | October 21, 2016

Have multiple versions of Python on the same system with Anaconda Python environments. It can be good to have multiple versions of python on the same system for testing, development, or just to use packages only available in Python 2.7. If you try to do a conventional install of multiple python versions, you will probably have problems with compatability and conflict between the versions. To get around this you can use Anaconda Python environments to manage your different python versions.

Create a new environment

A conda python environment can have its own packages and version of python, which you can keep separate from the system default, and other environments. To create a new environment just run:

conda create -n myenv python

This will create a new environment with the default python version, into which you can install packages as you need them.

Swapping Between Environments

To enter a virtual environment you have created you need to ‘activate’ it:

In windows this is done with

activate myenv

And in Linux/OSX:

source activate myenv

And similarly to deactivate:

In windows this is done with

deactivate myenv

And in Linux/OSX:

source deactivate myenv

Installing Packages Into An Environment

The above code will install a bare-bones python installation into the new environment. To install more than that you can either use a single line to install the package at the same time as creating the virtual environment:

conda create -n myenv python scipy

Or you can install the package into an existing virtual environment:

conda create -n myenv python scipy

You can also use pip in the new environment:

conda install -n myenv pip
source activate myenv
pip install my_package

Multiple python versions

One of the most powerful uses of multiple environments is being able to have multiple python versions installed and available on one systems. For this you need to specify the python version you wish to use in an environment. For example you might want to have a Python 2.7 environment available for using/testing python 2 scripts:

conda create -n py27 python=2.7

Find out more in the Conda docs