[mod_python] Unable to import modules in subdirs

Graham Dumpleton grahamd at dscpl.com.au
Thu Oct 27 18:42:55 EDT 2005

Jorey Bump wrote ..
> Brandon N wrote:
> > __init__.py's contents:
> > import sys
> > sys.path.insert( 0, '../' )
> ?

To expand on Jorey's "?" here with some general tips.

It is generally bad practice to extend sys.path from inside of modules
used by mod_python. At the least you must ensure you do not do it from a
module which is a candidate for automatic module reloading. This is
because every time the module is reloaded, the extension of sys.path
will occur yet again. Overall this will not stop the application
working, but if reloading occurs a lot, sys.path will become very long
and with many redundant entries in it.

The other things you need to be aware of is that you can not assume
anything about what the current working directory is when you are using
mod_python. Therefore you cannot use relative pathnames like "../" to
access any resource. This is because it will be resolved in relation to
the working directory which apache was run in, and not in relation to
where the Python code file resides.

If you need to specify a relative directory in relation to a code file
in order to access another resource, use something like:

  import os
  __here__ = os.path.dirname(__file__)

  directory = os.path.join(__here__,"..")


More information about the Mod_python mailing list