[mod_python] Still displaying source instead of results...

Jorey Bump list at joreybump.com
Fri Dec 3 09:27:15 EST 2004

Anthony M. Saffer wrote:
>>  <Directory /some/directory/htdocs/test>
>>   AddHandler python-program .py
>>   PythonHandler mptest
>>   PythonDebug On
> Okay, I've done that. Now, the request at least seems to actually GET to
> mod_python. But it is telling me it can't find the mptest module. I
> installed mod_python from source. Did I forget to download something that
> was not part of the source package?

You need to create mptest.py yourself, after the example in the manual.

>>You're not reading the right manual:
>>  http://www.modpython.org/live/mod_python-2.7.8/doc-html/
> This is the manual I am reading. I've read the entire "configuring apache"
> section and, with the exception of having to recompile apache, I've followed
> it to the best of my understanding. Did I miss something?

The lines you listed are from the 3.1 manual. You're using 2.7.

>>It's not a script, it's a module. It doesn't execute like a CGI, it gets
>>loaded into the interpreter. Therefore it doesn't need to be made
>>executable, only readable.
> I didn't mean I chmod +x the module. I meant that I did that to the .py
> script (I don't know why, it just was a last ditch effort I suppose). But I
> do understand what you are saying here.

I know it sounds like quibbling, but any code you run with mod_python is 
imported by python and treated as a module (with its own namespace). 
Therefore, mptest.py becomes the module mptest. This is in contrast with 
a python script that is made executable and run from the command line. 
Such a script has its own interpreter and automatically runs as the 
module __main__.

So, your .py applications are actually modules that will most likely 
share an interpreter and not run as individual scripts. This is very 
important to remember! If you grasp this at the beginning, you will 
spare yourself attempts to force apache and mod_python to behave the way 
you *think* it should.

I mention this to you in anticipation of future roadblocks you may 
encounter, especially if you are approaching this from a php or perl 
standpoint. Give your modules (files ending with .py) unique names 
*everywhere*, because they are imported into python like any other 
module in your path, with the directory names stripped.

Fortunately, there are advanced programming techniques that allow you to 
customize your environment to suit your style, including creating 
packages, and using frameworks.

Grisha's been kind enough to include a lot of technical details in the 
manual, and it merits a thourough reading. But I find that beginners get 
hung up on trying to create their own handlers and customizing apache, 
when the real gem is the Publisher handler. Here is a simple example to 
test your installation and get you started:

In httpd.conf:

<Directory /var/www/documentroot>
    AddHandler python-program .py
    PythonHandler mod_python.publisher
    PythonDebug On

Create test.py in your DocumentRoot:

def hello(req):
     x = "Hello, world!"
     return x

Restart apache (if you modified httpd.conf), point your browser at:


Note that I use AddHandler, unlike the example in the manual, which uses 
SetHandler. SetHandler forces all files in the directory to be handled 
by the PythonHandler. AddHandler limits this behaviour to a specific 
extension, allowing other types of files to peacefully coexist in the 
same directory:


This information is somewhat biased towards mod_python 2.7 and apache 
1.3. There may be important changes in recent versions. Good luck!

More information about the Mod_python mailing list