[mod_python] mod_python claims it cannot find a module

Graham Dumpleton grahamd at dscpl.com.au
Mon Jul 4 18:43:03 EDT 2005


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.

> Could mod_python "overload" the standard error message of import
> and spit something like a reference to a proper description
> with URL? ;-) I mean if it is possible to detect this situation
> at least somehow ...
>
> This is the "DirectoryIndex IRESite_web.py" requirement case:
> IOError: [Errno 2] No such file or directory:  
> '/home/mmokrejs/public_html/IRES2/index.py'
> This is the python PYTHON_PATH problem:
> ImportError: No module named web_settings
>
> The URL was always:
> http://aquarius/~mmokrejs/IRES2/IRESite_web.py?page=all

I haven't confirmed it, but because of the import error in IRESite_web,
mod_python.publisher would have tried instead to import "index.py" and
find a function in it called IRESite_web(). The bug in mod_python
was that if that then failed, the original import error message wasn't
being raised and instead that for index.py was. Thus, you couldn't see
what the original error was. Thus, you just hit a strange bug which is
giving wrong indication of the problem.

Setting DirectoryIndex will not help in this case as 
mod_python.publisher
ignores it at that point and will look for index.py regardless. The
DirectoryIndex directive is only used by Apache at the point that a
URL maps to the actual directory name.

Graham





More information about the Mod_python mailing list