What is virtualenv?

From the Python Guide:

A Virtual Environment, put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects.

For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0.

What is pip?

Pip is a package manager system for installing and managing Python packages.

Getting virtualenv

If you already have pip, then you can install virtualenv like so:

pip install virtualenv

Virtualenv includes pip, so all we need is to get virtualenv.

If you need an alternate way of installing it, refer to the docs.

Creating the virtualenv

Usually, you will want to have a virtual environment per project. So first, lets create a directory for the project and change directories to it.

mkdir my_project
cd my_project

Now, we will create the virtual environment. If you want to use your current python version, then you only need to run the following command.

virtualenv my_venv

If you would like to use a different Python version for your virtual environment, then you only have to add an argument -p to specify the location of the Python version you want to use.

Using the virtualenv

To use your virtual environment, you need to activate it first:

source my_venv/bin/activate

To exit the virtual environment, just type:

deactivate

Installing Python packages in virtualenv

Another reason for using virtualenv, besides having different Python versions for your projects, is that you can have different Python packages in separate environments. It makes it easier to manage multiple projects. :)

Installing packages example

Let's use the Flask package as an example of how to install packages in a virtual environment. Flask has the following (nested) requirements:

pip will automatically donwload and install ay package dependencies for you, if you have internet access.

pip install Flask

No internet?

Create a repository

If you don't have access to the internet from the server, then you will need to download the packages and their dependencies to install them manually. You can create a directory to store the package archives in and save them for future installations.

mkdir repo

Getting package sources

You can look for Python package sources in the Python Package Index (PyPI) using the search or browse functionalities.

Uninstalling packages

Uninstalling packages using pip is very straightforward. For example, if we were to uninstall Flask, we would do:

pip uninstall Flask

Getting a list of requirements

If you would like to know what Python packages you have installed in your virtual environment, then you can run the following command:

pip list

This will give you a list of the installed packages, like this:

distribute (0.6.34)
Flask (0.10.1)
itsdangerous (0.24)
Jinja2 (2.7.2)
MarkupSafe (0.21)
Werkzeug (0.9.4)

Exporting requirements

Using pip we can export a list of requirements (installed packages) for our Python environment.

pip freeze > requirements.txt

From our example, this creates a file with the following list of requirements:

Flask==0.10.1
Jinja2==2.7.2
MarkupSafe==0.21
Werkzeug==0.9.4
distribute==0.6.34
itsdangerous==0.24

Importing requirements

We can also use the requirements list to import the packages we want to install and specify a local directory to look for the packages.

pip install -r requirements.txt --no-index --find-links=[file://]<DIR>

The --no-index flag is for ignoring the package index and looking only at --find-links URLs or directories.

Further reading