[mod_python] Apache Configuration Help

Jorey Bump list at joreybump.com
Wed Apr 19 14:34:50 EDT 2006

David Worley wrote:
> I'm trying to get Apache to look up a couple directories for a python
> script, so in my httpd.conf file I've added the following:
> <Directory 'C:\Program Files\Apache Group\Apache2\htdocs\mg\css'>
> 	AddHandler python-program .css
> 	PythonHandler switch
> 	PythonDebug On
> </Directory>
> As far as I understand apache, this is saying any file in this directory
> will be handled by the file "switch.py".

No, it says that any request for a file ending in .css will be handled 
by a Python module named switch (using a function named 'handler' in the 

> Now, what I need is for switch.py to be locate in the \htdocs\directory.

Not necessarily. It is sufficient for switch.py to be anywhere in your 
(meaning apache's) module search path. mod_python will attempt to 
prepend the specified directory to sys.path, but it is not always 
desirable to place your handler within your DocumentRoot (all of 
mod_python's supplied handlers are typically installed in site-packages 
and available to all users, for example).

> Does anyone know how to go about this?

Well, obviously, the easiest way is to save the file in the specified 
directory. :)

Whenever I write a handler, however, and don't want it available 
globally to all users on the system, I put it in a special directory 
outside of my DocumentRoot and prepend that directory to PythonPath:

PythonPath "['/var/www/vhost/www.example.com/python'] + sys.path"
<Directory /var/www/vhost/www.example.com/website>
   SetHandler python-program
   PythonHandler myhandler

In this case, myhandler.py is in my virtual host's local python 
directory. Because I've used SetHandler, all files in the directory will 
be processed by the handler, regardless of extension. If you need to 
access other files in the directory without using mod_python, then use 
AddHandler as you did in your example.

Note that newer versions of mod_python support the use of 'mod_python' 
instead of the deprecated 'python-program' argument in (Add|Set)Handler 

More information about the Mod_python mailing list