Monday, January 24, 2011

The (python) way of the future: pip and virtualenv

I recently discovered the magic of pip and virtualenv. Pip is shaping up as a serious contender for replacement of distutils and easy_install. I haven't used it much yet, and will have to reserve judgement until I package a decent-sized project with pip.

Using pip is easy, although there is a bootstrapping problem that, ironically, I used easy_install to solve. If your distro packages a recent version of pip, you can use it to skip this bit.

sudo apt-get install python-setuptools
s easy_install pip
s pip install virtualenv

This gives you a virtualenv install. Virtualenv is brilliant, and has significantly influenced the way I write python. Virtualenv allows you to build a complete python environment in a directory that you 'activate' by running an in-built shell script that sets your python path environment variables. The are many advantages to this approach, but here are a few I have encountered:
  • Simplified testing - test your code against multiple versions of libraries and utilities
  • Keep separate dev and production environments on your dev machine, allowing you to test in a production environment without pushing the code to your actual production server.
  • The isolation provided by Virtualenv ensures you aren't depending on libraries on your dev box that aren't installed in production, and upgrading libraries won't break other applications

You can even create virtual environments with different versions of the python interpreter using:
virtualenv -p python2.6 2.6ENV

So, once you have a virtualenv, activate it and install yolk to give you a picture of what is inside the environment.
source .testenv/bin/activate
~> pip install yolk
~> yolk -l
Python          - 2.6.4        - active development (/usr/lib/python2.6/lib-dynload)
distribute      - 0.6.14       - active
pip             - 0.8.1        - active
wsgiref         - 0.1.2        - active development (/usr/lib/python2.6)
yolk            - 0.4.1        - active

Then go ahead and install the other packages you need:

pip install django

You can also build a requirements file to install exactly the same versions somewhere else:
pip freeze -E .testenv > requirements.txt

The holy trinity is apparently pip, virtualenv and fabric. Fabric is something I'll have to play with soon. There is a good tutorial on pip and virtualenv here, and using them with django here.

The only limitation/problem I have run into is that you can't move the virtual environment once it is created. There is a "--relocatable" directive to address this, but the webpage says that it is somewhat experimental at the moment.

No comments: