[mod_python] psp_site example doesn't work

Graham Dumpleton grahamd at dscpl.com.au
Mon Jan 24 16:45:38 EST 2005

Jorey Bump wrote ..
> There is no mechanism in Python to import a module by file path, so it's
> consistent, but confusing. The alternative would have been to force a 
> working directory onto the user, much like a cgi-bin, and prepend that
> to sys.path.

That "there is no mechanism in Python to import a module by file path"
isn't strictly true. There are actually a couple of ways of achieving it.

The import_module() method in order to work first has to find the actual
module. This is done using imp.find_module:

  f, p, d = imp.find_module(parts[i], path)

If path is None, the sys.path is searched for the module. If one sets path
explicitly, one can control exactly where a module is picked up.

This is in part how publisher works now, in that it passes a path argument
to the import_module() method to be the actual place where the module is
located so it only looks there.

The other way of loading a module from a specific path is using execfile()
to execute code in the context of a module created with imp.new_module().
This module will not though appear in sys.modules however, although not
sure why anyone might rely on this in the context of mod_python.

Now if you mean't specifically that one can't use the "import" statement to
import a module by path, then you are true.

Thus, import_module() already provides the means of picking a module
from the correct location, the problem is how it names modules and how
they get installed into sys.module and managed after that is the problem.


More information about the Mod_python mailing list