← All posts

Python. Building a Virtual Environment with virtualenv

Python and virtualenv logos

Virtualenv is designed to create a virtual environment (hereafter simply an “environment”) for your project. For example, your project might need a different version of Python from the one installed, or different versions of certain libraries. You can create a shared environment for several projects or a separate one for each project. This lets you install, change, and remove packages without affecting your other projects or the system environment.

This article covers version 1.7.1.2

Installation

First, you need to install it on your system, through PyPI:


$ sudo pip install virtualenv
$ sudo easy_install virtualenv

Or through Debian, Ubuntu:


$ sudo apt-get install python-virtualenv

Or through FreeBSD:


$ cd /usr/ports/evel/py-virtualenv
$ make install clean

Usage

Let’s start by looking at the most useful options; you can find the rest in man virtualenv:

--no-site-packages

Disable the use of the system site-packages (to isolate your environment completely from the system). For example, if you have “Django 1.3” installed on your system and use this option, that “Django” will not be available in the environment you create. В текущих версиях virtualenv эта опция используется по умолчанию, можете её не указывать.

--system-site-packages

This option is the opposite of the previous one: it makes the environment use packages installed on the system if they are not found in the environment.

-p PYTHON_EXE, –python=PYTHON_EXE

You can specify the version of the Python interpreter you need, but it must be installed on the system. If you omit this option, the default will be used (run which python to find out what yours is, but it will most likely be /usr/bin/python).

--distribute

Use Distribute. Опция используется по умолчанию.

--setuptools

Use Setuptools on top of Distribute. You can set the system environment variable VIRTUALENV_SETUPTOOLS to make Setuptools the default instead of Distribute.


$ export VIRTUALENV_SETUPTOOLS=true

--extra-search-dir=SEARCH_DIRS

The specified directories will be searched for packages when installing through setuptools/distribute/pip. This is useful when you do not want to install them from PyPI. Подробное описание тут.

--never-download

Do not use the network to retrieve packages. Used together with --extra-search-dir=SEARCH_DIRS. Подробное описание тут.

--relocatable

Make an existing environment use relative paths, so that you can move the environment’s directory and it will no longer depend on its location. However, this option is currently experimental. Подробное описание тут.

--prompt=PROMPT

The displayed environment name (prompt). For example, you can create one with the name (MY_VENV):


$ virtualenv --prompt="(MY_VENV)" <venv_name>
source venv/bin/activate
(MY_VENV)$

--clear

Used to clear packages and other changes from an existing environment.

Let’s create a shared environment (for several projects) without using the system site-packages:


$ mkdir ~/venv && cd ~/venv
$ virtualenv --no-site-packages <venv_name>

I often create an environment directly in the project directory:


$ cd ~/work/<project_name>
$ mkdir venv && echo "Virtualenv directory" > venv/README
$ git add venv && echo "/venv/" >> .gitignore && git add -f .gitignore
$ virtualenv --no-site-packages --prompt="(<project_name>)" <venv_name>

Once you have set up a new environment, you will have the following structure:


~/venv/<venv_name>
├── bin
│   ├── activate
│   ├── easy_install
│   ├── pip
│   ├── python
│   └── ...
├── include
│   └── python2.7 -> /usr/include/python2.7
├── lib
│   └── python2.7
│       ├── distutils
│       │   └── ...
│       ├── site-packages
│       │   └── ...
│       └── ...
└── local
    ├── bin -> /home/username/venv/<venv_name>/bin
    ├── include -> /home/username/venv/<venv_name>/include
    └── lib -> /home/username/venv/<venv_name>/lib

This gives you isolated versions of python, pip, and easy_install right away.

Working with the Environment

To get started, activate the environment like this:


$ source ~/venv/<venv_name>/bin/activate

To stop working with the environment (for example, to switch to the system python), run the following on the command line:


$ deactivate

You can also run python using its absolute path (this can be useful, for example, in cron). Here’s an example:


$ ~/venv/<venv_name>/bin/python manage.py

Installing Packages in the Environment

Activate the environment and run pip or easy_install:


$ source ~/venv/<venv_name>/bin/activate
$ pip install <something>
$ easy_install <something>

You can also install through pip without activating the environment:


$ pip install -E ~/venv/<venv_name> <something>

That’s all. I won’t cover virtualenvwrapper, since everything works well and is simple enough without it.

What Else Can I Read?

Comments are closed. Read the original discussion in Russian →