[mod_python] question on new import functionality (python 3.3.0b)

Graham Dumpleton grahamd at dscpl.com.au
Tue Jan 9 18:56:46 EST 2007


Detmar Meurers wrote ..
> Hi,
> 
> I'm transitioning to mod_python 3.3.0b and have a question about the
> description of import_module at
> http://www.modpython.org/live/mod_python-3.3.0b/doc-html/pyapi-apmeth.html
> where it states
> 
>      As a new feature in mod_python 3.3, when using the standard
>      Python ímport' statement to import a module, if the import is
>      being done from a module which was previously imported by the
>      mod_python module importer, it is equivalent to having called
>      apache.import_module() directly.
>  
>      For example:
> 
>         import name
> 
>      is equivalent to:
>     
>         from mod_python import apache
>             name = apache.import_module('name')
> 	    
> 
> In a setup where I am using the mod_python.publisher to
> load a file index.py as specified in an .htaccess file stating
> 
> DirectoryIndex index.html index.py
> AddHandler python-program .py
> PythonHandler mod_python.publisher
> 
> is that index.py being loaded using the new module importer?

Yes, modules imported by mod_python.publisher from the document
tree use the mod_python importing/caching mechanism.

> Can I just use plain "import" in such an index.py file loaded by the
> mod_python.publisher in order to import other modules using the new
> importing mechanism - or do I have to explicitly use
> "apache.import_module" in that index.py?

Yes. It will first look in the same directory for the desired module,
then in directories in __mp_path__ and the 'mod_python.importer.path'.
If found here, that module itself is also imported using the mod_python
importer, will be cached and also be a candidate for reloading.

If the module cannot be found in those places, the search is handed
off to the standard Python module importer which will look in sys.path,
this being able to be customised using PythonPath directive.

Note that the mod_python importer/cache doesn't work with Python
packages. Python packages should always be on sys.path and will be
handled by the standard Python module importer.

Graham


More information about the Mod_python mailing list