Python Virtual Environment

| | bookmark | email

Do you know what a Python Virtual environment is?

Python Virtual Environment: providing a means of having multiple isolated environments for the one Python installation on a system, such that it would be possible to run different applications on the same system, but using different sets of installed Python packages.

I've always asked myself how is this thing done in Python and Pylons with its go-pylons.py was the first I've found offering a solution. Then, I've found some more solutions.

Virtualenv, a solution developed by Ian Bicking, replacing the now deprecated workingenv and based inspired by virtual-python, seems to be the preferred solution (even if the comments on Ian's blog are still reporting issues).

virtualenv-dotcloud.png

Another option is documented by Graham Dumpleton and is introduced as the poor man's solution as it is pretty manual and it involves the manipulation of PYTHONPATH and the creation of a set of predefined symlinks.

As such setting the PYTHONHOME environment seems to be a simpler and more deterministic way of specifying where the Python lib directory is located, avoiding the need to perform fixups to the Python executable on MacOS X.

As to how to setup a Python virtual environment based on using the PYTHONHOME environment variable, for UNIX based systems it is just a matter of creating a parallel copy of the installed Python installation using symlinks. In some respects it is therefore quite similar to the original virtual-python, except that the Python executable itself is also just a symlink and not a copy.

Even if none of the above solutions seems to be 100% bullet proof, I do believe that they may prove quite useful for developing or maintaining projects relying on many dependencies and needing a stable environment.

Python 2.6 implements PEP370: Per-user site-packages directory. While the PEP doesn't directly address the virtual environments, it is introducing the possibility to control this directory through a new env variable PYTHONUSERBASE, which means that setting this variable to the desired location will in fact provide the solution.