[mod_python] importing from package under mod_python.publisher

Graham Dumpleton graham.dumpleton at gmail.com
Sun Apr 13 22:12:42 EDT 2008


2008/4/14 Sergiy Kuzmenko <s.kuzmenko at gmail.com>:
> Hi everybody,
>
>  Question about importing from package under mod_python.publisher.
>
>  Consider the following directory structure:
>
>  py
>     foo
>         __init__.py
>         bar.py
>     boo.py
>     test.py
>
>  Let's say we have the following in test.py:
>
>   import boo
>   import foo.bar
>
>  When running test.py from command line this works fine. However, under
>  mod_python only "import boo" works and importing from a subdirectory
>  via "import foo.bar" produces ImportError: No module named foo.
>
>  This seems to be a deviation form the standard python's behaviour.
>
>  What is even more confusing that mod_python manual
>  (http://modpython.org/live/current/doc-html/tut-what-it-do.html)
>  states the following when describing the PythonHandler directive: "If
>  not already done, prepend the directory in which the PythonHandler
>  directive was found to sys.path".

The documentation is no longer accurate then. It would be helpful if
you can create an issue at:

  https://issues.apache.org/jira/browse/MODPYTHON

pointing out this part of documentation and that it may be wrong.

>  When inspecting sys.path from a publisher defined like this:
>
>  <Directory /path/to/the/webroot/py>
>   AddHandler mod_python .py
>   PythonHandler publisher
>   PythonDebug On
>  </Directory>
>
>  /path/to/the/webroot/py is actually not the system path!

Read documentation for import_module() in:

  http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

This is what is used my mod_python module importer. It was rewritten
for mod_python 3.3.1 and one way in which it is different is that
directories are not added to sys.path for handler directory now. As a
consequence of this and how module importer path and sys.path are kept
separate, you can't/shouldn't go installing Python packages in the
document tree for your application.

The simple answer therefore is to place your Python package outside of
the document tree and extend sys.path directly or by using PythonPath
to refer to that other directory location.

Graham


More information about the Mod_python mailing list