Wednesday, 23 February 2011

Installing Django in VirtualEnv (Ubuntu - also with postgres & piston)

Due to work, I've recently switched from working with MS products and C# development to working in ubuntu developing python.

One of the first bits of advice I was given was to use virtualenv which allows you to isolate python environments, rather than just using the machines environment (and cluttering it/get clashes of versions etc)

The project I'm working on uses Django with a Postgres database, so here are the steps I needed to get it working. I'm using the default version of python however you can install different ones.
#install virtualenv
sudo apt-get install python-virtualenv

#install headers needed for psycopg2
#You can skip the next line if you're not using postgres as the database
sudo apt-get install libpq-dev python-dev

#create a folder for the virtual environment
mkdir mypyenv

#create the virtual environment
#no-site-packages ensures the environment doesn't also include packages installed at machine level
virtualenv --no-site-packages --distribute mypyenv

#switch to environment (environment name will be in brackets on terminal)
source mypyenv/bin/activate

#install django
pip install -E mypyenv Django

#I'm using piston also for my api - skip if you don't want it
pip install -E mypyenv django-piston

#install django
pip install -E mypyenv psycopg2
That should now be installed. Now just switch to your project folder and you should be able to work with it. If this is your first project then I'd highly recommend the official tutorial

Once you're finished using virtual environment, use the deactivate command to come out of it.