Change The Django Source Directory With A .pth File

John DeRosa recently wrote about using junctions (the Windows version of a symbolic link within Unix) to change the Django source directory. While that solution clearly works, I got the impression it seemed like a lot of hard work and John asked if there was a better way of handling that scenario.

If you’re wondering why a developer might want different versions of Django installed – it’s common if you need to upgrade an existing site from one version to another or if you offer a distributed application that is meant to work on the latest and also older versions of Django.

Under normal circumstances, developers place a copy of the Django source code underneath their ${install_path}\Lib\site-packages\ folder. While this works, it isn’t particularly convenient if you want or need to switch between the different versions. Under a unix based system, the common way of switching between the different versions is using a symbolic link; so you’d change the symbolic link from pointing from version A to version B. Unfortunately, in a Windows environment the concept of a symbolic link either doesn’t exist or isn’t common knowledge. As a by product, the average developer is only left with the option of copy and pasting the Django source code around in their file system or renaming folders – not particular convenient.

Fortunately, a little known feature of Python is that it supports special files with a .pth file extension. The .pth files or path files, are using by the site module (found under ${install_path}\Lib\) when the interpreters environment is initialised. The .pth files are standard text files, which contain one path per line, which when loaded are added into the sys.path collection.

A quick run down, place a django.pth file under ${install_path}\Lib\site-packages\, with a single line pointing towards your preferred Django source code version. When you need to change the version of Django you’re developing against – change the path within the file. Remember that if you have a Python interpreter open at the time, you’ll need to restart it and likewise if you’re running Django via something like Apache.

3 thoughts on “Change The Django Source Directory With A .pth File

  1. Thank you for this post! This does indeed sound easier than using junctions. BUT I can’t get it working on my windows box. Can you see what I’m doing wrong?

    Here’s my setup:

    * I have Python25\Lib\site-packages\django_versions\django_0.96.1, and …\django_versions\trunk\django. Each of these folders just what you think they contain. :-)

    * …\Lib\site-packages\django.pth contains:

    # Django path file

    The problem: Python can’t find Django. (E.g., “Import Error: No module names django.core”) I’ve tried forward and backward slashes, no difference. sys.path appears to have what it ought to have:

    >>> sys.path
    [”, ‘c:\\python25\\lib\\site-packages\\setuptools-0.6c3-py2.5.egg’, ‘f:\\tm\\trunk\\src’, ‘f:\
    \tm\\trunk\\src\\webcode’, ‘C:\\WINDOWS\\system32\\’, ‘c:\\python25\\DLLs’, ‘c:\\py
    thon25\\lib’, ‘c:\\python25\\lib\\plat-win’, ‘c:\\python25\\lib\\lib-tk’, ‘c:\\python25’, ‘c:\\
    python25\\lib\\site-packages’, ‘c:\\python25\\lib\\site-packages\\PIL’, ‘c:\\python25\\lib\\sit
    e-packages\\django_versions\\django_0.96.1’, ‘c:\\python25\\lib\\site-packages\\win32’, ‘c:\\py
    thon25\\lib\\site-packages\\win32\\lib’, ‘c:\\python25\\lib\\site-packages\\Pythonwin’, ‘c:\\py

    What am I doing wrong?

  2. John,

    A couple quick bits:

    * I have Python installed at C:\Program Files\Python25\
    * The Django repository ( is checked out into C:\Projects\Django\
    * My django.pth file, located under ${install_path}\Lib\site-packages\ contains a single line C:\Projects\Django\trunk\

    After doing that and opening a new Python shell, sys.path shows:

    [”, ‘C:\\WINDOWS\\system32\\’, ‘C:\\Program Files\\Python25\\DLLs’, ‘C:\\Program Files\\Python25\\lib’, ‘C:\\Program Files\\Python25\\lib\\plat-win’,’C:\\Program Files\\Python25\\lib\\lib-tk’, ‘C:\\Program Files\\Python25’, ‘C:\\Program Files\\Python25\\lib\\site-packages’,’C:\\Projects\\Django\\trunk’]

    One difference I can see at the moment, is that I’m using an absolute path to the Django source code and you’re using a relative one. Of course, once the site package reads either value in, they are both showing what I’d consider an appropriate value.

    Just to be sure, you’d need to have your django_0.96.1 directory pointing at the source found in, ie that a folder named django exists under it. If you’re pointing that directory into, I don’t believe it’ll work.


Comments are closed.