[mod_python] mod_python 3.3.1 / python 2.5 / apache.import_module(__init__) help

Graham Dumpleton graham.dumpleton at gmail.com
Sun Nov 23 18:33:22 EST 2008


2008/11/24 Adam Venturella <aventurella at gmail.com>:
> The docs here:
> http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
>
> Mention this bit:
> If an __init__.py file is present and it was necessary to import it to
> achieve the same result as importing the root of a true Python package, then
> __init__ can be used as the module name. For example:
>     from mod_python import apache
>     module = apache.import_module('dirname/__init__')
>
>
> I think I a bit confused about how I understand it.  I read that as follows:
> myModule = apache.import_module('dirname/__init__')
>
> where dirname/ contains the following files:
> __init__.py
> application.py
> navigation.py
> and dirname/__init__.py contains:
> from mod_python import apache
> application = apache.import_module('application');
> navigation  = apache.import_module(navigation');
>
> So, in my mind, doing this:
> myModule = apache.import_module('dirname/__init__')
>
> should let me:
> from myModule import application
> OR
> from myModule import navigation
>
> That, however, is not the case as I get an ImportError:
> ImportError: No module named myModule
>
>
> can someone correct my understanding of how apache.import_module(__init__)
> is supposed to work / what it's benefit is?

Ideally you shouldn't. If this is a proper Python package then put it
outside of the document tree on sys.path somewhere and use normal
Python 'import' statement to import it.

Trying to get __init__.py to work is a bit of a fudge and if there are
absolute imports within the package back onto itself, or even relative
imports to sub packages, they will not work as it isn't being treated
as a proper Python package.

Graham


More information about the Mod_python mailing list