Virtualenv in Plone

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 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
plone$ source ./bin/activate
(plone)plone$ python -V
Python 2.4.6
(plone)plone$ which 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 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.


~ by Liz on May 22, 2009.

One Response to “Virtualenv in Plone”

  1. I hardly ever use the activate command. Usually, I just call the virtualenv python explicitly… “bin/python”. In the case of a buildout, if you run “bin/python”, then the buildout script generated by the bootstrap will then always call the correct python.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: