Virtualenv is a project that is indispensable for most Python devs these days. I am writing down some tips here so mainly for personal reference, and because I found them useful.

Virtualenv & Upstart¶

For my ReadTheDocs project, I was wanting to run celery as a background process that processes documentation building. I’m running Ubuntu, so their built-in upstart service seems like a logical choice. I really like upstart because of it’s simple configuration, but it is rather undocumented (this Stanzas) page is a useful starting point).

Carl Meyer pointed out to me that in order to get inside the context of a virtualenv, you don’t need to munge your pythonpath or anything, but simply run the correct script from inside the virtualenv. So a simple /path/to/virtualenv/bin/django-admin.py celeryd was all that was needed to get inside the virtualenv’s context.

This also true of the python executable inside your python directory. /path/to/virtualenv/bin/python will allow you to run any python script inside of that virtualenv’s context.

I also wanted to be running my jobs as the user for that site, so sudo is the correct tool for that. The final file ended up looking like this:

description "Celery for ReadTheDocs" start on runlevel [2345] stop on runlevel [!2345] #Send KILL after 20 seconds kill timeout 20 script exec sudo -i -u docs django-admin.py celeryd -f /home/docs/sites/readthedocs.com/run/celery.log -c 2 -B end script respawn