Django Autotest on Hudson/Jenkins with VirtualEnv

September 29, 2011

I wanted to run an autotest (./ test) on my Django project, after first installing dependencies into a virtualenv. Here's the setup I used.

Jenkins Environment Variables

In the build, set the environment variables so that the virtualenv is added to the PATH, and the workspace is added to the PYTHONPATH:


Jenkins Build - Execute Shell

In the Jenkins build, we need to execute a shell script:

if [ -d ".env" ]; then
  echo "**> virtualenv exists"
  echo  "**> creating virtualenv"
  virtualenv --no-site-packages .env

source .env/bin/activate
pip install -r requirements.txt
cd project_folder
./ test --settings=settingsautotest

Note that I'm using a specific settings file for the autotest.

Also note that we're loading dependencies into the virtualenv using a requirements.txt file. This file for me contains:


All the other dependencies I had are included in the project folder and version controlled along with my project source.

We create the virtualenv using the --no-site-packages flag in order to limit our dependency on the system installed packages.


I had a problem installing psycopg2 under the virtual env - it failed with the following error:

Error: pg_config executable not found.

This was solved using by installing a few extra packages:

# apt-get install libpq-dev python-dev

Once this was sorted I got the following error:

psycopg2.ProgrammingError: autocommit cannot be used inside a transaction

This was solved by limiting the psycopg2 version to 2.2.1 in the requirements.txt file, rather than the default 2.4.2.