Motivation

Creating an isolated environment with Virtualenv

Use virtualenvwrapper! Since these notes were written, Virtualenvwrapper has a emerged as a tool that simplifies the user of Virtualenv. Check out its documentation.

Virtualenv is a tool that creates isolated Python environments. More precisely, it allows creating virtual environments that have different versions of Python and Python libraries. This makes it possible to test applications using different set of libraries, or checking if an upgrade of a library will cause errors, without affecting the computer system's global Python installation with all its site-packages.

Note that Virtualenv can only contain Python packages. If an environment needs other types of software, this software must be installed separately and globally on the computer system.

The recommended way to install Virtualenv is from PyPi (the Python Package Index) using pip:

Terminal> sudo pip install virtualenv

The pip tool itself can be installed from pure Python source via the standard sudo python setup.py install command. On Debian systems (including Ubuntu) one can install pip by sudo apt-get install python-pip.

The virtualenv script creates a new virtual environment in a destination directory here named venv:

Terminal> virtualenv venv

By default, virtualenv will symlink to the system’s site-packages if the user installs a package in the virtual environment that is already installed globally on the computer system. To create a totally isolated environment one can use the -–no-site-packages switch when creating the environment:

Terminal> virtualenv --no-site-packages venv

One can apply the -p flag to specify which Python executable to use as the python program in the environment:

Terminal> virtualenv -p /usr/bin/python2.6

The next step is to activate the virtual environment. To do this, we source the activation script from the bin subdirectory of the newly created venv directory:

Terminal> cd venv/
Terminal> source bin/activate

Successful execution of the activate script changes the prompt in the terminal window so that the prompt is prefixed with the name of the virtual environment one is using:

Terminal> source bin/activate
(venv)Terminal>

Packages we install will now be installed in the lib/pythonX.Y/site-packages directory within the environment, where X.Y is the Python version. First we install yolk, which is a simple tool for listing all installed Python packages:

(venv)Terminal> pip install yolk
(venv)Terminal> yolk -l

The latter command lists all Python packages installed in the current virtualenv. At this point, there is nothing more than the essential tools like Python and pip, and of course yolk itself.

Let us also install the Python web framework Django:

(venv)Terminal> pip install Django

Using yolk we see that the Django version we installed is 1.5.1:

(venv)Terminal> yolk -l | grep Django
Django          - 1.5.1        - active

Within a Virtualenv environment, the local python interpreter and local packages are always used:

(venv)Terminal> which python
/some/path/to/venv/bin/python
(venv)Terminal> python -c 'import django as m; print m'
<module 'django' from '/some/path/to/venv/local/lib/...'>

What can be installed by pip install? The above examples have installed Python packages whose names and details are present in the Python Package Index (PyPi). One can also install from tarballs as long as the root directory of the tarball contains a setup.py file to do the installation:

(venv)Terminal> pip install ../some/dir/package.tar.gz
(venv)Terminal> pip install http://some.net/dir/package.tar.gz

Installation directly from the source in a version control system is also possible (if a setup.py resides in the root directory):

(venv)Terminal> pip install -e \
                git+https://github.com/hplgit/odespy.git#egg=odespy

The syntax is pip install -e vcs+URL#egg=packaname, where vcs is the name of the version control system (hg, git, svn, cvs, bzr).