Skip to content

Virtual Environments

Virtual Environments

Using virtual environments is good standard practice when using Python. A virtual environment is a standalone Python interpreter and isolated installed packages unique to that environment.

Benefits of Virtual Environments

Virtual environments keep the Python version and stand-alone package versions all in the same place. If two different projects use two different versions of Python or use two different versions of an installed 3rd-party package, virtual environments separates these two installations separate.

The Python version and installed packages in a virtual environment can be shared with others in the form of a requirements.txt or environment.yml file. These files can be shared to produce the same environment on another machine or so the same environment can be reproduced by another programmer.

Get the current Python interpreter

The file path to the current running Python interpreter can be accessed using the sys module. sys.prefix will output the system path for the running Python interpreter

In [1]:
import sys
sys.prefix

Out[1]:
'/home/tribilium/anaconda3/envs/book'

Get a list of installed packages

A list of installed packages in the current environment can be accessed using the site module. site.getsitepackages() will return the file path where the site-packages are stored. The code below will lists all of the installed packages using the os module and os.listdir() to list the contents of the directory where the site-packages are stored.

import os
import site
print(site.getsitepackages())
os.listdir(site.getsitepackages()[0])
['/home/peter/anaconda3/envs/book/lib/python3.6/site-packages']

['jinja2',
 'asn1crypto',
 'ipython_genutils-0.2.0-py3.6.egg-info',
 'alabaster',
 'yapf-0.22.0-py2.7.egg-info',
 'requests',
 'Pygments-2.2.0-py3.6.egg-info',
 'urllib3',
 ...]

Virtual environments with conda

The Anaconda Prompt is useful for installing third party packages and creating virtual environments. To create the new virtual environment, issue the following command at the Anaconda Prompt:

> 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>. Replace <env_name> with the name of the new virtual environment. Including python=3.6 ensures the virtual environment has an up to date version of Python.

After issuing the command, the output will be something like:

The following NEW packages will be INSTALLED:

    certifi:        2016.2.28-py36_0
    pip:            9.0.1-py36_1
    python:         3.6.2-0
    setuptools:     36.4.0-py36_0
    vs2015_runtime: 14.0.25420-0
    wheel:          0.29.0-py36_0
    wincertstore:   0.2-py36_0

Proceed ([y]/n)? y

Type y to confirm and create the new virtual environment. To use the new virtual environment, it needs to be activated. A virtual environment is activated by typing the command below. Note that <env_name> needs to be replaced by the name of the virtual environment.

>  conda activate <env_name>

You know you are in your virtual environment <env_name> when (<env_name>) is in parenthesis at the start of the Anaconda Prompt:

> (webscrape)

To deactivate an active environment, type:

> conda deactivate

If you see the (<env_name>) in parenthesis before the command prompt, that means the new virtual environment is set up and active. A list of your virtual environments installed on your machine can be accessed using the conda info --envs or conda env list commands.

> conda info --envs

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

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

To exit the virtual environment, use the command conda deactivate. If conda env list is run again, there will not be an asterisk * in front of webscrape. That's because webscrape is not the active virtual environment.

> conda env list

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

Virtual environments with pip and virtualenv

Virtual environments can also be created with pip, and a 3rd-party python environment manager virtualenv. virtualenv is a Python package used to create and manage virtual environments. To use virtualenv, first make sure it is installed using pip at the commandline.

$ pip install virtualenv

Once installed, virtualenv will create a new virtual environment with the command below. Note <env_name> needs to be replaced by the name of the new virtual environment.

$ virtualenv <env_name>

To activate the virtual environment use the command:

$ source bin/activate

Once active, the virtual environment name will be shown in parenthesis () before the command prompt

(<env_name>)$

The location of the Python interpreter being used by the currenly active virtual environment can be found with:

(<env_name>)$ which python
/Users/peter/<env_name>/bin/python

To exit the virtual environment use the command deactivate:

(<env_name>)$ deactivate

When the virtual environment is no longer active, there will be no environment name before the command prompt. A check for the current Python interpreter will return a different path than before when the virtual environment was active

$ which python
/usr/bin/python