Question about the "new" module

Graham Dumpleton grahamd at dscpl.com.au
Fri Dec 29 20:24:05 EST 2006


skip at pobox.com wrote:
> Gabriele> I'm using Python 2.5 to develop a simple MVC framework based
>     Gabriele> on mod_python. To load my controllers, I create new modules
>     Gabriele> using the "new" module like this:
>
>     Gabriele> # ....
>     Gabriele> my_module = new.module("random_name")
>     Gabriele> my_module.__file__ = module_path
>     Gabriele> exec open(module_path, "r") in my_module.__dict__
>
>     Gabriele> then I initialize the class defined inside the module and call
>     Gabriele> a method of this class based on the HTTP request.
>
> Why use the new module?  Why not call __import__() or execfile()?  Details
> on their use are here:
>
>     http://docs.python.org/dev/lib/built-in-funcs.html

Or why not use mod_python.apache.import_module() from mod_python
itself. It is designed as a way of users importing specific modules,
including the capability for modules to be reloaded if they have been
changed. See:


http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfModuleImporting

as well as the mod_python documentation.

Is recommended though to use mod_python 3.3 as soon as you can though
if you want the module reloading to always work. You can find
descriptions of problems with the module reloading in older versions of
mod_python at:


http://www.dscpl.com.au/wiki/ModPython/Articles/ModuleImportingIsBroken

Note that module importer in mod_python 3.3 has been enhanced some what
over prior versions in addition to fixing problems. An important new
feature is being able to load modules based on their path with modules
with same names in different directories being properly distinguished,
something which is quite useful where modules are used in web
applications to represent pages. You may want to look at documentation
for it at:


http://www.modpython.org/live/mod_python-3.3.0b/doc-html/pyapi-apmeth.html

Graham




More information about the Python-list mailing list