[mod_python] PythonHandler does not appear to append directory to import path

Gregory (Grisha) Trubetskoy grisha at modpython.org
Tue Sep 9 10:41:55 EST 2003

Gerrat -

You're right, it doesn't add the directory to the path if PythonPath is
specified. The code that decides this has no way of knowing whether the
handler was specified via an Apache directive or req.add_handler(). It
just follows the rule that PythonPath overrides everything.

The rationale for this all-overriding behaviour of PythonPath is that you
can use it make sure that the directory where the handler is specified is
_not_ in sys.path. (Otherwise, if the directive directory was _always_
added, there would be no way to achieve this)

You can do two things:

1. Add directory_to_append_to_sys_path to PythonPath
2. Get rid of PythonPath. I see a lot of people using it where they really
don't need to. See if you _really_ need it.

What *I* need to do is adjust the documentation of add_handler() to
clarify that dir has no effect when PythonPath is specified.


On Mon, 8 Sep 2003, Gerrat Rickert wrote:

> I'm using mod_python 3.03, apache 2.047, and python 2.2.3 on a win2K machine
> & am trying to use the publisher handler.  The problem is when I try to add
> an additional handler via:
> req.addhandler("PythonHandler", testModule, directory_to_append_to_sys_path)
> The error I am getting is:
> Mod_python error: "PythonHandler testModule"
> Traceback (most recent call last):
>   File "D:\Python22\lib\site-packages\mod_python\apache.py", line 340, in
> HandlerDispatch
>     module = import_module(module_name, config)
>   File "D:\Python22\lib\site-packages\mod_python\apache.py", line 516, in
> import_module
>     f, p, d = imp.find_module(parts[i], path)
> ImportError: No module named testModule
> Looking through the HandlerDispatch function of apache.py, the relevant code
> is:
> if config.has_key("PythonPath"):
> ...
> else:
>     dir = hlist.directory
>     if dir and (dir not in sys.path):
>         sys.path[:0] = [dir]
> This code doesn't appear to add my directory
> (directory_to_append_to_sys_path) to the sys.path if I have "PythonPath" in
> my httpd.conf file.  Importing my module fails because of this.  I'm not
> sure if this is just a misunderstanding on my part or has already been
> addressed (in which case I apologize for posting).  As far as I can see, the
> "else" portion of this statement should be executed regardless of whether
> PythonPath is in the config file or not.  Can anyone else confirm this?
> Thanks,
> Gerrat Rickert
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list