Using virtualenv to switch Python version under Cygwin

My dev machines usually come with python 2.6 or 2.7 installed. Unfortunately, our testing / prod machines run old Lenny Debians with python 2.5.

Since: - I don’t want to develop on a different version of python than the test and prod machines, - I don’t want to downgrade my dev machine’s python, I need a way to switch python versions.

Fortunately, virtualenv allows this and more.

Installing Python 2.5 under Cygwin

Since I already have a Python 2.6 installed on my system, I don’t want to break it by overwriting one of its file. But most of the Python system comes in folders and files named after the version, so it won’t overwrite anything.

The simplest way I found was to download and extract the release/python/python-2.5.5-1.tar.bz2 binary package from one of the Cygwin ftp mirrors.

Then copy most 2.5 files to /usr.

$ cp lib/* /usr/lib
$ cp include/* /usr/include
$ cp bin/*2.5* /usr/bin

This way I keep my 2.6 installation, and have a 2.5 version for virtualenv to use.

Note: this is the only Cygwin specific topic in the article. On a real Linux you probably can apt-get --no-overwrite install python2.5 and be done. (Yes, –no-overwrite doesn’t exist, sorry.)

Installing virtualenv

You need easy_install if you don’t have it already:

$ curl http://peak.telecommunity.com/dist/ez_setup.py | python

Then install virtualenv:

$ easy_install virtualenv

Creating a Python 2.5 environment

First I created a directory to put my environments:

$ mkdir ~/.virtualenvs

Then create an environment with the -p (or --python) option set to “python2.5”:

$ virtualenv -p python2.5 --no-site-package ~/.virtualenvs/python2.5
Running virtualenv with interpreter /usr/bin/python2.5
New python executable in /cygdrive/c/Users/sra/.virtualenvs/python2.5/bin/python2.5
Also creating executable in /cygdrive/c/Users/sra/.virtualenvs/python2.5/bin/python.exe
Installing setuptools............................done.
Installing pip...............done.

The –no-site-package option shields you from your system’s /usr/lib/python2.5/site-package, useful when you want Dev = CI = Prod.

Using virtualenv

virtualenv generates an activate script that allows you to switch environments.

$ python --version  # my system's python
Python 2.6.5

$ source ~/.virtualenvs/python2.5/bin/activate  # Abracadabra...

(python2.5)
$ python --version  # welcome python 2.5.5!
Python 2.5.5

Note that the activate scripts needs to be sourced, and that the prompt now indicates the current environment.

virtualenv also installs setuptools and pip in new environments, and all the python packages you install in an environment don’t interfere with other environments.

(python2.5)
$ pip install sqlalchemy
Downloading/unpacking sqlalchemy
...
Cleaning up...

(python2.5)
$ python -c 'import sqlalchemy'

(python2.5)
$

Switching back to 2.6 is easy, just call the deactivate shell function (defined by the activate script):

(python2.5)
$ deactivate

$ python --version
Python 2.6.5

Note that my system doesn’t know about python packages installed in the python2.5 environment:

$python -c 'import sqlalchemy'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named sqlalchemy

An interesting wrapper

Typing things like:

$ source ~/.virtualenvs/python2.5/bin/activate

can be hard to remember, so Doug Hellman wrote virtualenvwrapper, a useful wrapper that simplify this to:

$ workon python2.5

with tab completion, and a mkvirtualenv function to create environments in the default .virtualenvironments dir.

blog comments powered by Disqus