Skip to content

Virtual Environments

Virtual Environments

Using virtual environments is standard practice in Python. A virtual environment is an isolated installation of Python with associated packages. When you use virtual environments, one project can have a separate version of Python and packages. Another project can use a different virtual environment and therefore have a separate version of Python and packages. By utilizing virtual environments, two projects on the same computer can use different versions of Python and different versions of packages.

Create a virtual environment with the Anaconda Prompt

To create the new virtual environment, open the Anaconda Prompt and issue the command:

> conda create --name env_name python=3.6

The conda create command builds the new virtual environment. The --name env_name flag gives the new virtual environment the name env_name. Including python=3.6 ensures the virtual environment has a current version of Python.

The following output or something similar results:

The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0           
    certifi:         2018.11.29-py36_0      
    libedit:         3.1.20170329-h6b74fdf_2
    libffi:          3.2.1-hd88cf55_4       
    libgcc-ng:       8.2.0-hdf63c60_1       
    libstdcxx-ng:    8.2.0-hdf63c60_1       
    ncurses:         6.1-he6710b0_1         
    openssl:         1.1.1a-h7b6447c_0      
    pip:             18.1-py36_0            
    python:          3.6.8-h0371630_0       
    readline:        7.0-h7b6447c_5         
    setuptools:      40.6.3-py36_0          
    sqlite:          3.26.0-h7b6447c_0      
    tk:              8.6.8-hbc83047_0       
    wheel:           0.32.3-py36_0          
    xz:              5.2.4-h14c3975_4       
    zlib:            1.2.11-h7b6447c_3      

Proceed ([y]/n)?

Type y to confirm and create the new virtual environment.

Activate a virtual environment

To use the new virtual environment env_name, it first needs to be activated:

> conda activate env_name

The virtual environment is active when you see (env_name) > in parenthesis at the start of the prompt:

(env_name) > 

Install packages in a virtual environment

When a new virtual environment is created, no packages are installed by default. If you use the Anaconda distribution of Python, the base environment contains about 600 packages that come with Anaconda. But a fresh new virtual environment will just have Python installed, no other packages.

To install a package into the virtual environment (env_name), first, make sure the environment is active ((env_name) before the prompt). Package installation is accomplished with the conda install command followed by the package name. To install Matplotlib into the virtual environment type:

(env_name) > conda install matplotlib

Multiple packages can be installed with the same command. To install both NumPy and Jupyter use:

(env_name) > conda install numpy jupyter

Deactivate a virtual environment

To deactivate an active environment, use:

(env_name) > conda deactivate
>

When the virtual environment is deactivated, the prompt looks normal >, with no environment name in parenthesis before it.

List your virtual environments

View a list of your virtual environments using the command conda info --envs or conda env list.

> conda activate env_name
(env_name) > conda info --envs

# conda environments:
#
base                     /home/tribilium/anaconda3
env_name               * /home/tribilium/anaconda3/envs/env_name
matplotlib               /home/tribilium/anaconda3/envs/matplotlib

Notice the * asterisk on the line with env_name. The virtual environment with the * is currently active.

To exit the virtual environment, use the command conda deactivate.

(env_name) > conda deactive

If you run conda env list again, there is no * in front of env_name. That's because the env_name virtual environment is no longer active.

> conda env list

# conda environments:
#
base                  *  /home/tribilium/anaconda3
env_name                 /home/tribilium/anaconda3/envs/env_name
matplotlib               /home/tribilium/anaconda3/envs/matplotlib

Remove a virtual environment

Remove a virtual environment with the command conda remove --name env_name --all. Exit out of the environment to want to remove before you remove it.

Check to see if the virtual environment you want to remove is present:

> conda env list

# conda environments:
#
base                  *  /home/tribilium/anaconda3
env_name                 /home/tribilium/anaconda3/envs/env_name
matplotlib               /home/tribilium/anaconda3/envs/matplotlib

Remove the virtual environment called env_name.

> conda remove --name env_name --all

Proceed ([y]/n)?

Type [y] to remove the environment. View a list of your virtual environments again. Note that env_nameis no longer in the list.

> conda env list

# conda environments:
#
base                  *  /home/tribilium/anaconda3
matplotlib               /home/tribilium/anaconda3/envs/matplotlib