Jan Mészáros reflections on technology and society

19Nov/090

Python virtual environment

Hosting multiple Django projects requires to keep deployment and libraries in control. Imagine, that an one year old Django application is written for Django 0.96, but new projects are written using the actual version 1.1.1. It is possible, of course, to rewrite the old applications to be compatible with the actual version, but rewriting many projects might consume a lot of time.

There is a smart solution to deploy applications using different versions of Django on only one production server. This can be done easily using the "virtualenv" library, which allows you to create unlimited number of python environments including the site-packages directory.

At first, install the virtualenv library with easy_install:

  1. easy_install virtualenv

Then, create a new Python virtual environment:

  1. virtualenv --no-site-packages /usr/local/lib/my_new_python_environment

The --no-site-packages parameter avoids access to the global site-packages dir to the virtual environment. Finally, activate the new python environment with command:

  1. source /usr/local/lib/my_new_python_environment/bin/activate

Deactivation of the activated environment is done by following command:

  1. deactivate

Detailed how-to on deploying Django project using the new virtual environment through mod_wsgi is available at http://jmoiron.net/blog/deploying-django-mod-wsgi-virtualenv/

Tagged as: , No Comments
21Oct/092

Django multiple database support with DB switch feature

Our Django installation is capable to connect to as many databases as desired and switch between databases in according to session data. We reached this feature overriding several internal Django classes, according to recommendations on http://groups.google.com/group/django-users/msg/d1d7e0af565cc444?. All multiple database features are done without changes in original Django code.

Partial how-to can be found on http://kfalck.net/2009/07/01/multiple-databases-and-sharding-with-django too.

The solution is successfully tested for more than one month in real-world application and this application is going to be used into production environment soon.

For implementation details or questions leave a comment below.