[mod_python] import_module

Ron Alford ronwalf at umd.edu
Wed Apr 14 09:00:50 EST 2004

Since this seems to come up over and over again with the 
apache.load_module, I thought I'd mention my solution:

I have a pair of functions,

pathToModuleName(filename), which returns a module name from a given 
filename, and
loadModule(filename, modulename), which returns a module loaded into the 
given modulename.

My general pattern for using these is
mname = PREFIX + '.' + pathToModuleName(req.filename)
module = loadModule(req.filename, mname)

You can find my implementation here:

I can slap any license you want on it for this section (but my wife and 
I are having a debate between BSD-like and LGPL for the rest of the code 
base :).

An alternative implementation would be to have a global in apache that 
matched filenames to  a randomly generated module name, thus avoiding a 
bunch of the code that I needed.  I think this method could be easily 
integrated into the mod_python code-base.  Anyone see a problem with this?


More information about the Mod_python mailing list