Virtualenv has been recommended to me on several occasions, particularly when I was having issues with my Python install on my MacBook. I was having some trouble wrapping my head around it, and never seemed to be able to get it to work. The main thing I was missing was the “activate” command.
I never seem to have Python 2.4 as the main Python on the machines I use anymore. If I forgot to use python2.4 when running bootstrap.py the first time, it would lead to a chain of predictable errors and some frustration. Frustration because I knew what I had done, and that it had happened yet again.
Enter virtualenv, the virtual environment.
Create a virtualenv environment suitable for Plone:
$ virtualenv -p /usr/bin/python2.4 plone
The -p option lets you specify which Python command to use within the virtualenv. This command created a folder named plone.
Browse inside the plone/bin folder and take a look at the activate file. When you source this file, all of this is run. Add any extra path or alias information you might need in here, in the same format you would use in your shell.
Let’s take a look at what this file does to your shell. (I am running Ubuntu Jaunty Jackalope)
$ cd ~/plone
plone$ python -V
Python 2.6.2
plone$ which python
/usr/bin/python
plone$ source ./bin/activate
(plone)plone$ python -V
Python 2.4.6
(plone)plone$ which python
/home/liz/plone/bin/python
Note the (plone) at the start of the prompt, use this to remind yourself that you are operating inside a virtualenv.
Now we have a default python that is the version we want, without effecting the rest of the system. What else might we want to handle locally? How about grabbing the PIL?
(plone)plone$ easy_install --find-links http://www.pythonware.com/products/pil/ Imaging
That gets us a copy of the pil that only lives in the virtualenv. You can see it in ~/plone/lib/python2.4/site-packages This sort of thing is very useful if you need to have several versions of a package on your box, or just want to try out a package and not worry about having to uninstall it.
You can work with Plone stuff either in this directory, or in another location. I tend to put everything Plone related in my Plone directory for organization purposes, but the virtualenv will work the same no matter where your code lives on your box.
Posted in Plone
Tags: Plone, Python, virtualenv