[mod_python] Building a web application using Cheetah andmod_python

Graham Dumpleton grahamd at dscpl.com.au
Mon Oct 30 20:46:23 EST 2006

Dirk van Oosterbosch, IR labs wrote ..
> Thanks,
> > Using current versions of mod_python available there is no way of  
> > doing it except
> > perhaps by using the Vampire extensions for mod_python.  
> > Alternatively, use
> > version 3.3 of mod_python out of Subversion repository or wait  
> > until it is released.
> >
> > [...]
> >
> > This works even if 'from bar import bar' is used as they intercept  
> > the 'import'
> > statement and when an import is performed from inside a module  
> > which was
> > already imported using the module importer, it will use the module  
> > importer for
> > the 'import' as well. Ie., no need to use apache.import_module()  
> > explicitly. If the
> > module being imported using 'import' is in another directory  
> > though, it is necessary
> > to specify an option declaring the search path for the modules.  
> > This is a path which
> > is distinct from PythonPath as new importer will not use any module 
> > in PythonPath
> > as a candidate for reloading as that causes lots of problems.
> Then, I guess it won't do it's magic for __import__(modulename)  :-(

No that still works as it hooks into the Python importer hooks which
is even below that.

> The way I am building my system, I'll need to call
> 	module = __import__(modulename, globals(), locals(), [name])
> since I don't know the name of the module (and of the object) until  
> runtime.
> Will there be a cure for this too?

In mod_python 3.3 there is a much better way of doing that, you simply
supply an absolute path name to the apache.import_module() command
instead of the module name. There are even tricky ways of specifying
paths relative to the current modules directory, or even the directory
where the handler was specified in order to avoiding having to hard
code absolute path names.

Where it is done at global scope, changes made to the child will cause
the parent to be reloaded as well next time it is needed. If the import is
done from inside a handler, then the fact that apache.import_module()
is called each time causes reload checks to be done anyway.

The only thing is that it returns the whole module so you then have
to use hasattr()/getattr() to look for and get a reference to a contained

> (I wouldn't have a problem so much with issuing a special command to  
> make mod_python reload all modules, if it just wasn't "apachectl  
> stop ... apachectl start" ;)

Restarting Apache shouldn't be required, nor do you need to tell it to
reload modules at a particular time, it will reload modules as it sees
they have changed. You still have to be careful in changing files if you
need to change more than one at a time. In that case you might manage
by manipulating PythonAutoReload in a .htaccess file, but for a production
site you are still better off having a totally different directory containing
everything, changing an Alias directive and restarting Apache. Much safer
that way. Auto reloading is still more only for during development or
if your site is non critical.

I have been putting together an example of exactly how to integrate
Cheetah with mod_python 3.3 in the best way which I'll explain in an
article at some point and make available. No point making it available
yet if you aren't using mod_python 3.3 source from subversion.


More information about the Mod_python mailing list