[mod_python] Vampire for module reloading

Graham Dumpleton grahamd at dscpl.com.au
Thu Jul 7 18:50:49 EDT 2005


Gonzalo =?ISO-8859-1?Q?Sainz-Tr=E1paga?= wrote ..

> On Thu, 2005-07-07 at 02:47 -0400, Graham Dumpleton wrote:
> > As someone pointed out already, any local imports of modules which
> > are a part of your web application should not use the "import" statement
> > but should instead explicitly use apache.import_module(). Although
> > they said to replace all uses of "import", you should only do so for
> your
> > own code. Do not use apache.import_module() for modules which are
> > a part of the standard Python distribution. The apache.import_module()
> > function also doesn't work very well with packages, so avoid using
> > packages for your own code.
> 
> What do you mean by "packages"?

When I say package, what I am referring to is where Python modules are
structured in a directory where the top level directory has __init__.py in it
denoting to Python that it is actually a directory based package rather
than a file based module.

Ie., module would be a single file of form:

  module.py

and package is where a directory is used and there could be multiple
parts to it:

  package/__init__.py
  package/submodule.py
  package/subpackage/__init__.py

> I am using sqlobject and a few others
> which are installed in my shared hosting directory, inside a "packages"
> file.

The sqlobject system is structured as a package.

> What I am doing is altering sys.path in a config.py file that is
> imported by all the other files in my application.

Setting sys.path explicitly from inside Python code files executing under
mod_python is generally not a good idea. At least it could be a problem
in a multithread MPM.

> I will drop this and replace is with import_module() and the
> corresponding path for the files like you recommended, but what do you
> mean by "doesn't work very well"?

Doesn't work very well, as in don't expect automatic module reloading to
work as you think it should when you use "import_module()" on a package.
Also, if you access submodules or subpackages from a package, you will
end up with redundant imports occuring even though nothing has changed.
There are also various other issues when you get complicated packages.
See just posted article on problems with module importing system:

  http://www.dscpl.com.au/articles.html

Because "sqlobject" is a package, therefore don't use "import_module()"
with it. In this case, sqlobject isn't one of your own and would normally be
in Python site-packages directory. For any externally sourced code like this
stick it in a special directory separate to your own code. This may actually
be what your "packages" directory is already for.

For these externally sourced packages you don't have much choice but to
explicitly set PythonPath directive to define their location and continue to
use the "import" statement.

As for your own code, it is better not to organise it as packages if you want
to be able to use "import_module()" with it.

Anyway, I am sure others will disagree with me on all this. :-)

Graham


More information about the Mod_python mailing list