[mod_python] ImportError issue

Graham Dumpleton graham.dumpleton at gmail.com
Wed Dec 3 17:35:45 EST 2008


2008/12/3 Reuben A Christie <christie at knewco.com>:
> Hi all, I am pretty novice user of mod_python. and there is an issue that I
> am experiencing with mod_python scripts for past 2 days that I am not able
> to solve.
>
> my directory structure is as following,
>
> DocumentRoot :  /usr/local/apache2/htdocs/
> i have a directory structure for mod_python scripts under DocumentRoot is,
>
> launch.py
> python_test/p1/test1
> python_test/p2/test2
>
> in apache config, i have added following
>
> <Directory /usr/local/apache/htdocs/python_test >
>       AllowOverride All
>       Order allow,deny
>       allow from all             AddHandler mod_python .py
>       PythonHandler launch
>       PythonDebug On
> </Directory>
>
> and launch.py looks like this,
>
> import os.path
> from mod_python import apache, Session, util
>
> def handler(req):
>       request = os.path.splitext(os.path.basename( req.uri ))[0]
>       req.content_type = 'text/plain'
>       req.send_http_header()
>
>       status = apache.OK
>       if request != None:
>               mod = __import__(request)

This is the wrong way of going about it. See below.

>               if request == "test1":
>                       status = mod.handler(req)
>        return status
>            basically this script should act as gateway for rest of the
> python scripts .
>
> when I test it with, http://localhost/python_test/test1.py it does not work
> (i get Not found message) and if I do http://localhost/python_test/p1 i get
> the source code of the script appear.

For configuration as written, the launch.py file is in wrong location.

First off suggest that launch.py not be in htdocs as then someone can
download source code to your handler.

Presuming you are using mod_python 3.3.1 (if not then upgrade), move
the launch.py file elsewhere and then change configuration to use:

  PythonHandler /some/path/launch.py

Where path is location of launch.py outside of htdocs.

Also don't use __import__. You are better off using import_module()
from mod_python. That way you can just supply a path to file to import
and not have to worry about PythonPath and lots of other module
madness. See:

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

Graham


More information about the Mod_python mailing list