[Django]-What is a general process/concept to determine what to add to PYTHONPATH to resolve import issues?


From your question I assume that you are quite new to Python, so let’s start at the beginning.

My advice would be to look at the traceback that Django generates and search for a line which looks like this: “ImportError: No module named somemodule”. This tells you the name of the missing module.

Once you know the name of the module you can try to see if the missing module is listed in PyPi (Python Package Index) by using easy_install.

Try running:

easy_install somemodule

If you’re lucky then the missing module will be installed into your python installation’s site-packages directory (possibly /usr/lib/python2.6/site-packages, depending on where your python is installed). The site-packages directory is where all 3rd party modules should be installed to, and all modules in this directory are always import-able and on the PYTHONPATH.

It’s possible that your code is trying to import something that is not available on PyPi, in which case Google is your best option :p

If easy_install doesn’t work (or it’s missing) then it’s possible that you don’t have the setuptools python module installed, which you can get from here: http://pypi.python.org/pypi/setuptools



If you are using eclipse, she can automatically configure python interpreter (includes all libraries which are installed system-wide). You probably have done this step.

If you are still facing import problems you probably did not installed libraries system-wide. Or you have modified python paths and removed default directories.

Usually there is a setup.py with python library. It will install that library into your system:

python setup.py install

Some libraries which comes with setup.py can be installed from python package index with pip.

If library does not provide setup.py script then you will need to deal with it manually.

So basic rule about python libraries is: if you did not installed them system wide, then you have to add them to python-path manually.

If you do not want to install bunch of libraries system-wide, tools like virtualenv or buildout can help.

Virtualenv creates an environment where you can install libraries as if you would install them globally:

[path-to-virtualenv]/bin/python setup.py install

and your library will install to:


Buildout works a bit differently. You specify a list of packages you want to install in a configuration files, and it will generate script files with paths fixed. Buildout can also generate configuration settings for pydev project. Buildout is automization tool with its own way to create virtual environment. Buildout does more things than virtualenv, but can be difficult to deal with at first.



If you’re installing modules system wide (site-packages or similar), you probably won’t have a problem running them on your system, so I’m going to discuss the PYTHONPATH of your project.

The directory of the file that is your entry path into your program is added to the PYTHONPATH, along with all the system defined PYTHONPATHS. To get to any script within that directory structure, each directory must contain a file called __init__.py, which signals that that directory is a module within your program.

            models.py  # unreachable
            views.py   # unreachable

If run python entrypoint.py you will be able to access app1, because all folders from entrypoint.py to app1 contain an __init__.py file. However, app2 can’t be imported, since it is not visible as a module.

If you have a directory that is a module, you must ensure that it contains an __init__.py, or that /path/to/app1 is within sys.path itself, which can then be imported simply with import models.

Leave a comment