2

I need to use Django 1.2 for one of my projects.
I also already have several projects running on Django 1.3 on the same server, and I need to keep them running.

Is there a way to only use 1.2 for a specific project?
Both sites run on Apache via mod_wsgi.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • [buildout](http://jacobian.org/writing/django-apps-with-buildout/) and [virtualenv](http://www.virtualenv.org/en/latest/index.html) ftw! – Davor Lucic Jun 14 '11 at 12:54

3 Answers3

8

Consider installing Django inside a virtualenv. This will make the installation of python modules, including Django, completely independent of the rest of the system. This way, if you have multiple virtualenvs, you can have as many versions of Django installed (one per env).

To use a virtualenv, you shold edit your index.wsgi and add the following two lines before any other line that imports or references Django:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

This is assuming that you are not using different version of the python interpreter itself, which is possible using virtualenv, but would make things quite a bit more complicated.

FelipeFG
  • 520
  • 3
  • 5
  • how to I use virtualenv with Apache? – Dan Abramov Jun 14 '11 at 13:02
  • @gaearon: I've edited it into the answer, because of the block of code. [This link](http://www.virtualenv.org/en/latest/index.html#using-virtualenv-without-bin-python) gives the full explanation on what these lines do, from the virtualenv site. – FelipeFG Jun 14 '11 at 15:47
2

Yes. Django is just a regular Python module living in your site-packages. So when you import django.something, the way Python decides which version to load is by walking down the Python path (import sys; print sys.path) in order and looking for a directory called django (with something.py or something/_init_.py inside). It loads the first one it finds. So the quickest method is to modify this Python path before starting your project, which can be conveniently done with an environment variable named PYTHONPATH.

So for your Django 1.2 project, install Django 1.2 in site-packages/django-1.2 and then:

# run Django 1.2 for old app that I don't have time to update
cd ~gaearon/src/old-django-project
env PYTHONPATH=/Library/Python/2.6/site-packages/django-1.2 ./manage.py runserver

For all other (Django 1.3) projects, simply install Django as normal, which makes it the default:

# all other projects use the system default Django 1.3
cd ~gaearon/src/current-django-project
./manage.py runserver

For production servers you won't use runserver, but the PYTHONPATH will work wherever you invoke Python (i.e. flup, or manage.py runfcgi). If invoked from mod_python there is an Apache configuration directive to modify the Python path (see mod_python documentation).

Or you could use virtualenv.

Ingmar Hupp
  • 2,409
  • 18
  • 22
  • This is indeed an option, but a bit more complicated one. Whenever you're working on Django 1.2, don't forget to set the PATH correctly with something like `export PATH=/Library/Python/2.6/site-packages/django-1.2/bin:$PATH` so that you run the correct django-admin script for creating a 1.2 project. This isn't necessary for ./manage.py thou. – FelipeFG Jun 14 '11 at 12:43
  • @Igmar: thanks for the thorough answer. However I use Apache with mod_wsgi, not `runserver`. Is there a way to configure it this way? – Dan Abramov Jun 14 '11 at 13:00
  • 1
    @gaearon: A quick glance at the mod_wsgi documentation reveals [WSGIPythonPath](http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonPath), although it seems that won't do what you want if you have mod_wsgi 2.x. What will work with any version is simply modifying the path in Python before any Django imports happen, which would be in the wsgi script (the one mod_wsgi invokes to talk to your app). Django's [deployment documentation](https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/) has an example on how to do that. – Ingmar Hupp Jun 14 '11 at 19:51
1

When I need to do stuff like this I have found that virtualenv combined with virtualenvwrapper helps a lot.

> mkvirtualenv django1.2
> cd django-1.2-dist-dir
> python setup.py install 
> mkvirtualenv django1.3
> cd django-1.3-dist-dir
> python setup.py install 

Now both Django versions are installed in their own virtual environments. To use a specific one do:

> workon django1.2

or

> workon django1.3
Arlaharen
  • 3,095
  • 29
  • 26