[mod_python] The new module loader

Graham Dumpleton grahamd at dscpl.com.au
Sat Apr 22 08:14:37 EDT 2006

On 22/04/2006, at 4:08 PM, Dan Eloff wrote:

>> How are the files in this directory (_config.py) being imported in
>> the first place?
>> Is your pyserver.py file importing them explicitly using
>> apache.import_module()?
> Yes, that's exactly it.

If your pyserver.py is effectively acting as a dispatcher for  
multiple pages
based on URL, and it is using apache.import_module() to load an actual
handler module file from your "web" directory by full path name, ie.,
something like:

   stub = posixpath.splitex(req.path_info)[0]
   file = os.path.join("C:/My Documents/PYROOT/web", stub) + ".py"
   module = apache.import_module(file)

Change it to:

   directory = os.path.dirname(__file__)
   stub = posixpath.splitex(req.path_info)[0]
   file = os.path.join("C:/My Documents/PYROOT/web", stub) + ".py"
   module = apache.import_module(file, path=[directory])

It is important you be providing a full pathname as first argument to  
apache.import_module() as by doing so it triggers a special different
interpretation for the "path" argument.

Namely, when the module is specified by a full or absolute pathname,
as opposed to just the module name, the "path" argument becomes a
search path which is embedded within the loaded module and which
is consulted when sub imports are done from within that module.

Using this feature, you can then embed the directory the config module
is in as part of the module search path for the "web" module and using
the "import" statement or apache.import_module() to load it should
then work.

Note that the search path is only embedded within the module directly
loaded by apache.import_module() when this is done. The search
path is not inherited by any modules it may then in turn import.

Also note that this feature is an area which still has to be properly
thought out as to how it should work or be accessible. How it is
currently exposed is handy in some circumstances, but possibly
not as generally useful as it could be. How the ability to use it is
exposed may well change in time. One has to be really careful though
with such a feature because of the possibilities of introducing
unpredictable behaviour from when a module is imported from
different places with potentially different search paths defined. This
sort of path difference problem is a big problem with the current
importer, so rather want to avoid it.

I'll probably talk again about these search path issues when I finally
get around to addressing Jorey's email.

> If you want some unasked for info on the housing, ....

Which country is this???? Doesn't quite reflect my situation here in Oz.
Anyway, with all those property terms in there, probably lucky my spam
filter didn't flag it. :-)


More information about the Mod_python mailing list