views:

22

answers:

3

Hello,

I'm exploring mod_python and I'm having trouble with the package importing.

I've a structure like this:

my base dir
     |
     +- __init__.py  
     +- index.py    
     +- package (directory)
        |
        +- __init__.py
        +- package.py (file)

and an Apache Virtual Host like this:

<VirtualHost *:80>

        ServerAdmin root at localhost
        ServerName myname
        DocumentRoot /path/to/my base dir

        <Location />
                DirectoryIndex index.html index.py
                Options Indexes MultiViews FollowSymLinks
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
        </Location>

</VirtualHost>

in the index.py file I've something like this:

from package.package import myobject
....
....

When I load index.py from Apache, I get a 500 Internal Server Error as follows:

ImportError: No module named package.package

What am I doing wrong?

Cheers, Ivan

A: 

Make sure your PYTHONPATH is correct: http://www.modpython.org/live/mod_python-3.2.8/doc-html/dir-other-pp.html

Krumelur
Please don't refer to mod_python 3.2.8 documentation. Thinks changed a lot with module importing in 3.3 and so the old documentation wouldn't be relevant if they are using newer mod_python.
Graham Dumpleton
A: 

Firstly, if you're just beginning with Python web deployment you should not be using mod_python. It is now officially a dead project and is deprecated. Use mod_wsgi instead.

The actual issue with your code is that you haven't put your root directory on the Python path, so mod_python doesn't know where to find it. DocumentRoot is used for static documents, not code - in fact you shouldn't set it to your base dir, as that is insecure and may lead to the contents of your Python code being exposed over the web, which is not what you want.

Instead, use the PythonPath directive:

PythonPath "['/path/to/my base dir']"
Daniel Roseman
Thanks!1) I'd like to switch to mod_wsgi. I've a simple web app without any application framework. Can you suggest me some resources for beginners?
eradan
2) As you suggested I fixed my problem with this configuration:<VirtualHost *:80> ServerAdmin root@localhost ServerName myapp.com PythonPath "sys.path+['/path/to/mybasedir']" Alias / /path/to/mybasedir <Location /> DirectoryIndex index.html index.py AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Location></VirtualHost>now it works but this is not so different from using the DocumentRoot directive.
eradan
@eraden: Have a look at Flask (http://flask.pocoo.org). This has the simplicity that people who have used mod_python.publisher would find attractive.
Graham Dumpleton
A: 

In mod_python 3.3, the structure of Python code files for mod_python.publisher is not a package. Ensure you read:

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

Specifically, the documentation about import_module() as it explains how code importing works.

Graham Dumpleton