Lately, I have been doing a lot of development in Python, as the crud UI layer of Votizen.com is built in Python/Django. As you develop with Python you will invariably want to install packages built by yourself and the community. You can manually install packages somewhere on your PYTHONPATH , or use pip to automatically install them. This works great until you have two packages with the same name or you are writing two applications that require different versions of the same package.

Fortunately, Python developers have already addressed this problem by allowing you to create isolated virtual Python environments, using virtualenv, where you can muck around without affecting your other Python environments. This article will explain how to setup Python using virtualenv on a unix based system (I've verified this on Ubuntu and OSX).

Getting ready

For the sake of this article we will explain how to create an isolated Python environment for a fictitious project named myproject .

Most Unix systems come with a version of Python already installed (although it may be quite out of date). If that is not the case, or it is the wrong version, then you can get the latest source code at the Python Releases page. Once that is done, the rest of this article will explain how to install and use virtualenv .

How to do it…

Install the virtual environment:

easy_install virtualenv

Create your virtual environment (I recommending creating a "~/projects" directory and executing there):

mkdir ~/projects cd ~/projects virualenv myproject

Modify your bash profile so that you can enter your python project context with a single command ( vi ~/.bash_profile :

export PROJECTS_HOME=~/projects export MYPROJECT_HOME=${PROJECTS_HOME}/myproject alias start_myproject="cd ${MY_PROJECT_HOME};source bin/activate"

Re-source your bash profile then type start_myproject to enter the project's virtual environment context.

How it works…

You have now created a directory ~/projects/myproject/ which will have the directories: bin , include , and lib . The bin directory will contain applications, such as easy_install and pip . The include directory will contain a symbolic link to your original Python directory. The lib directory will contain the isolated Python instance to be used by this project. You can put your projects that use this virtual environment anywhere, but I recommend organizing them directly in this directory and/or in subdirectories.

The context is started by executing source ~/projects/myproject/bin/activate . This call is handled automatically by the alias start_myproject . Once you have activated your context, then Python will look to the ~/projects/myproject/lib/python2.7/site-packages/ for loading and installing packages. You can manually install packages there or use a package manager like pip .

There's more…

The Python package management app, pip, has been referenced several times in this article, because it comes with virtualenv and is awesome. It makes installing and upgrading packages trivial, as well as automatically managing dependencies for you. Whenever you use pip , make sure you are in the correct Python virtual environments, then use one of the following techniques to install packages.

To install the latest version of package:

pip install fabric

To upgrade an already installed package:

pip install fabric --upgrade

To install a specific version of a package:

pip install fabric==0.9.3

To install from a git repository:

pip install -e git+https://github.com/votizen/django-storages.git#egg=django-storage

Replace the github directory with your own URL package ( https://github.com/votizen/django-storages.git in this case) and name the egg ( django-storage in this case) to whatever you want to call the package. These will be downloaded to the ~/projects/myproject/src/ directory.

These techniques are all useful, but if you have a distributed environment, used by several developers, it is better to create a requirements.pip file, that includes all your Python dependencies.

Here is a simple requirements.pip file:

BeautifulSoup==3.2.0 Django==1.3 Fabric==0.9.3 MySQL-python==1.2.3 PIL==1.1.7 South==0.7.3 django-compressor==0.5.3 boto==2.0b4

To install packages from a file run:

pip install -r requirements.pip

I recommend adding this last as part of your build scripts.