[mod_python] mod_python claims it cannot find a module

Martin MOKREJŠ mmokrejs at ribosome.natur.cuni.cz
Mon Jul 4 19:52:06 EDT 2005

Graham Dumpleton wrote:
> On 05/07/2005, at 8:15 AM, Martin MOKREJŠ wrote:
>> Graham Dumpleton wrote:
>>> Enable mod_python handler inside the context of a "Directory" directive
>>> and not at global scope within Apache configuration file.
>>> <Directory /home/mmokrejs/public_html/IRES2>
>>> AddHandler mod_python .py
>>> PythonHandler mod_python.publisher
>>> PythonDebug On
>>> </Directory>
>>> If web_settings is in that actual directory, it should then be found.
>> Well, you are right I have it at the moment in the "global scope".
>> I used to have it under the "Directory" directive. why doesn't that
>> approach work?
> If the PythonHandler directive appears at global scope or within the
> context of a Location directory, there is no relationship to a physical
> directory. When there is a relationship to a physical directory, then
> mod_python will add the root directory where PythonHandler is specified
> for automatically into the Python module search path.
> Having done this, if any publisher module uses the "import" statement
> explicitly to import a module where the module is in that root directory
> where PythonHandler was specified for, the module will be found okay.
> Note that this only applies for child modules in the root directory where
> PythonHandler directive was specified. If you had publisher modules in
> a subdirectory and it used "import" to get to a child module in the sub
> directory, it will not work as the subdirectory isn't added to the
> Python module search path.
> Because of problems with mixing "import" with the mod_python module
> loader, you are actually better of using:
>   from mod_python import apache
>   import os
>   directory = os.path.dirname(__file__)
>   web_settings = apache.import_module("web_settings",path=[directory])
> Here you are explicitly telling it which directory to get the module
> from and avoid all problems with things not being found in the Python
> module search path. You should only use this for loading in your own
> modules though, not standard Python modules.

Graham, how can I fork the code so that for normal "console" use it would do
normal import and that when running under mod_apache it would run this trick?
I use the modules also for command-line tests and some utilities.
What variable should I look for in __dict__.keys()? ;-)

More information about the Mod_python mailing list