[mod_python] The new module loader

Graham Dumpleton grahamd at dscpl.com.au
Fri Apr 21 01:44:10 EDT 2006


Graham Dumpleton wrote ..
> Dan Eloff wrote ..
> > <Directory />
> >     SetHandler mod_python
> >     PythonHandler PyServer.pyserver
> 
> The new module importer completely ignores packages as it is practically
> impossible to get any form of automatic module reloading to work
> correctly with them when they are more than trivial. As such, packages
> are handed off to standard Python __import__ to deal with. That it even
> finds the package means that you have it installed in sys.path. Even if
> it was a file based module, because it is on sys.path and thus likely to
> be installed in a standard location, the new module importer would again
> ignore it as it leaves all sys.path modules up to Python __import__
> as too dangerous to be mixing importing schemes.
> 
> Anyway, that all only applies if you were expecting PyServer.pyserver to
> automatically reload upon changes.

BTW, that something outside of the document tree, possibly in sys.path,
is dealt with by Python __import__ doesn't mean you can't have module
reloading on stuff outside of the document tree. The idea is that if it is
part of the web application and needs to be reloadable, that it doesn't
really belong in standard Python directories anyway. People only install
it there at present because it is convenient.

The better way of dealing with this with the new module importer is to
put your web application modules elsewhere, ie., not on sys.path. You then
specify an absolute path to the actual .py file in the handler directive.

 <Directory />
     SetHandler mod_python
     PythonHandler /path/to/web/application/PyServer/pserver.py
     ...

Most cases I have seen is that people use packages purely to create a
namespace to group the modules. With the new module importer that
doesn't really need to be done anymore. That is because you can
directly reference an arbitrary module by its path. When you use the
"import" statement in files in that directory, one of the places it will
automatically look, without that directory needing to be in sys.path,
is the same directory the file is in. This achieves the same result as
what people are using packages for now but you can still have module
reloading work.

If you need a working example of this let me know.

Graham



More information about the Mod_python mailing list