Conda environments

What is a virtual environment

A virtual environment [1] is a named, isolated, working copy of Python that maintains its own files, directories, and paths so that you can work with specific versions of libraries or Python itself without affecting other Python projects.

Creating a virtual environment

To create a virtual environment run:

conda create -n <virtual-environment>

Then Conda will ask for permission to proceed, press y. Remember, the environment usually is created in the user’s home directory .conda/envs/environment-name>.

Activate a virtual environment

To activate or switch into a virtual environment run:

source activate <environment-name>

Remember that it’s not necessary to deactivate the actual environment to switch into another environment. Also, your shell should change to indicate the name of the current conda environment (<environment-name>)user@hostname.

Note

To list all your environments use: conda env list

Deactivate a virtual environment

To end a session in the current environment run:

source deactivate

It is not necessary to specify the environment name, it takes the current environment and deactivates it.

Delete a virtual environment

If the user wants to delete a non-using environment run the following command:

coda env remove -n <environment-name>

Export an environment

To export an environment file use the following command after activate the environment to export, the file will be create in the actual directory:

conda env export > <environment-name>.yml

Then to use an environment that was exported run:

conda env create -f <environment-name>.yml

where -f means the environment export file.

Other useful commands

Create a requirements file

The requirements file is a way to get pip to install specific packages to make up an environment [2], also this file list all the packages that are used to documentation. In Conda, you can create this file using:

conda list -e > requirements.txt

References