Wednesday 4 February 2009

python (and Django) development environment

I like virtualenv and pip. Sadly, they don't work so well with python2.3 - virtualenv will work if you manually copy a version of subprocess.py from a python2.4/2.5 machine into site-packages. pip just won't work (it uses @property)...

Anyway, together they make it very easy to deploy a development environment for Django. Here are some notes (using python 2.4 on SLC5):

Starting - install virtualenv and pip
Use easy_install to install virtualenv and pip (a bit of a bootstrap problem - but this is the last time to use easy_install on your OS python directories !)
$ easy_install virtualenv
...
...
$ easy_install pip
Searching for pip
Reading http://pypi.python.org/simple/pip/
Reading http://pip.openplans.org
Best match: pip 0.3.1
Downloading http://pypi.python.org/packages/source/p/pip/pip-0.3.1.tar.gz#md5=78102ddbb040a183dd361b5d432cdf88
...
...
Adding pip 0.3.1 to easy-install.pth file
Installing pip script to /usr/bin

Installed /usr/lib/python2.4/site-packages/pip-0.3.1-py2.4.egg
Processing dependencies for pip
Finished processing dependencies for pip



First example - just installing django with virtualenv
This uses virtualenv to set up a virtual environment, and standard easy_install to install django.

$virtualenv devo
New python executable in devo/bin/python
Installing setuptools.............done.

$easy_install django
Searching for django
Reading http://pypi.python.org/simple/django/
Reading http://www.djangoproject.com/
Reading http://www.djangoproject.com/download/1.0.1-beta-1/tarball/
Best match: Django 1.0.2-final
Downloading http://media.djangoproject.com/releases/1.0.2/Django-1.0.2-final.tar.gz
Processing Django-1.0.2-final.tar.gz
...
...
...
Installing django-admin.py script to /tmp/jamesc/devo/bin

Installed /tmp/jamesc/devo/lib/python2.4/site-packages/Django-1.0.2_final-py2.4.egg
Processing dependencies for django
Finished processing dependencies for django

$. ./devo/bin/activate

(devo)$python
Python 2.4.3 (#1, Mar 13 2008, 13:35:20)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
h[1] >>> import django
h[1] >>>
Second Example - Doing it with pip
pip can call out to virtualenv to set up the environment it needs, so what is above becomes even easier -
13:45:03[jamesc]vtb-generi$pip install -E devo django
Creating new virtualenv environment in /tmp/jamesc/devo
New python executable in /tmp/jamesc/devo/bin/python
Installing setuptools...done......
Downloading/unpacking django
Downloading Django-1.0.2-final.tar.gz (4.6Mb): 4.6Mb downloaded
Running setup.py egg_info for package django
Installing collected packages: django
Running setup.py install for django
changing mode of build/scripts-2.4/django-admin.py from 644 to 755
changing mode of /tmp/jamesc/devo/bin/django-admin.py to 755
Successfully installed django
Third Example - requirements files in pip
pip has a nice feature of requirement files - here you can put a bunch of packages you want and they get downloaded - either via PyPi or from subversion repositories - this is useful for django add-ons, since many of them are in google code.

Here are two useful requirements file - firstly a generic development tool one : devo-requires.txt
# Devo tools
ipython
pylint
logilab-common
logilab-astng
# Doc tools
docutils
Jinja
Pygments
sphinx
mock

# django + deps
Django
python-ldap
#MySQL-python
pysqlite
And a second with some more specific django tools django-requires.txt
django-pagination
django-reversion
django-gatekeeper
django-batchadmin

# No egg available
-e svn+http://django-command-extensions.googlecode.com/svn/trunk#egg=django-extensions
-e svn+http://django-notification.googlecode.com/svn/trunk#egg=django-notification
# Should use latest SVN version , not package
-e svn+http://django-sphinx.googlecode.com/svn/trunk/#egg=djangosphinx
-e svn+http://django-tagging.googlecode.com/svn/trunk#egg=django-tagging
To install :
$ pip install -E devo -r devo-requires.txt
$ pip install -E devo -r django-requires.txt
You'll need to make sure you have the relevant -devel rpms installed e.g. openldap-devel, sqlite-devel, mysql-devel.

No comments: