[mod_python] Importing package under mod_python

Jorey Bump list at joreybump.com
Tue Mar 25 08:55:02 EDT 2008

Dominique.Holzwarth at ch.delarue.com wrote, at 03/25/2008 05:37 AM:

> I've got some problems with importing modules from packages when using
> mod_python.
> Currently I'm using the Publisher Handler which is configured like this:
> <Directory "C:\Program Files\Apache Software
> Foundation\Apache2.2\htdocs\python"> 
> 		AddHandler mod_python .py
> 		PythonHandler mod_python.publisher		
> 		PythonDebug On 
> </Directory>
> My directory structure looks like this
> python/
> 	main.py
> 	database/
> 		__init__.py
> 		common.py

The new importer in mod_python will ignore packages in the published 
directory and does not automatically add this location to the path when 
it falls back to the regular Python importer. In a nutshell, avoid 
trying to publish packages.

There are other ways to include your package in the Python path. In 
fact, yours is a good candidate, since I'm guessing you have included 
database connection credentials, which you should NEVER put in your 
DocumentRoot. Any number of scenarios could result in your credentials 
being exposed.

I create many of my mod_python applications as packages, and simply 
publish a wrapper that imports one or two functions from the package. In 
my virtual host configuration, I add the directory that contains all of 
my packages to the path:

   PythonPath "['/var/www/www.example.com/python'] + sys.path"

Note that this directory is outside the DocumentRoot, which might be:


So, for a specific directory, the resulting configuration might be:

   # Set Python path for entire virtual host
   PythonPath "['/var/www/www.example.com/python'] + sys.path"
   # All files in this directory use the Publisher handler
   <Directory /var/www/www.example.com/site/app>
     SetHandler python-program
     PythonHandler mod_python.publisher
     PythonDebug On
   # Custom virtual handler, using module in Python path
   <Location /virtual>
     SetHandler python-program
     # this module (can be a package) must be in the Python path!
     PythonHandler virtual
     PythonDebug On

With this approach, various handlers in a specific host can share a 
common directory of modules, without needing to add it to the system's 
site-packages directory, where it would be too public. There is also a 
security advantage in storing database connection credentials outside 
the DocumentRoot, where they are less likely to be exposed (due to a 
misconfiguration, or absent-mindedly moving your code to a new host 
before mod_python is properly configured). You simply import the modules 
as you would in any other Python application.

I'm happy with this approach, but the biggest disadvantage is that it 
won't take full advantage of automatic module reloading, so moving new 
code to a production environment might still involve restarting apache 
httpd. For alternative approaches and an excellent explanation of how 
packages are affected by the new module importer, see Graham's response 
in this thread:


More information about the Mod_python mailing list