Python and the Virtual Environment

Warning

This page needs review and likely revision


“Virtualenv is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.” - Hitchhiker’s Guide to Python

Install virtualenv

pip install virtualenv

Note

if setting up an RHEL 5 server, you need to install python26 & setuptools first

(walkthrough) and use easy_install with python26, i.e.

sudo python26 /usr/lib/python2.6/site-packages/easy_install.py virtualenv

Create a virtualenv for mdid3

Once installed, make a directory for storing your virtualenv - if you’ve installed mdid to /var/local/mdid consider putting your virtualenv in /var/local/virt to keep things close, but remember to chown/chgrp the directory so the user mdid can use it

sudo mkdir /var/local/virt
sudo chown mdid:staff virt
virtualenv-2.6 -v --no-site-packages virt/mdid3

Ok, so now all you have to do is switch to the virtualenv:

source /var/local/virt/mdid3/bin/activate

and your terminal prompt should go from $ to this: (mdid3)$ (if your prompt is just $)

To switch back to your default system python, just type deactivate and you’ll be back to normal.

bash tip

Add this to your .bash_profile to activate by typing mdid3:

alias mdid3='source /var/local/virt/mdid3-py26/bin/activate'

then type

source ~/.bash_profile

to activate your new command.

Now activate

source /var/local/virt/mdid3-py26/bin/activate

or (if you listened to my advice about adding the alias to your bash shell)

mdid3

Re-get your python libraries

  1. pip install -r mdid-dep.txt
  2. Sip coffee
  3. (optional, not your choice) Curse when you realize it didn’t work exactly.

Reconfigure your apache.conf & wsgi script to use the virtual env

You’ll need to add something like this to your wsgi script and then restart your webserver (safety note: a typo or wrong path will result in downtime)

See http://code.google.com/p/modwsgi/wiki/VirtualEnvironments for more information.

httpd.conf

WSGIPythonHome /var/local/virt/mdid3-py26/

rooibos.wsgi

import site
site.addsitedir('/var/local/virt/mdid3-py26/lib/python2.6/site-packages')