[mod_python] PythonImport: Can someone please draw a diagram forthis idiot?

Jorey Bump list at joreybump.com
Wed Sep 14 07:51:45 EDT 2005

jamestmcneill-python at yahoo.co.uk wrote:
> Nicolas,
> Thanks for that. Unfortunately I find that it doesn't really get me any
> further. Using mpglobals.cur.execute just lets me know that it isn't
> defined either ("NameError: global name 'mpglobals' is not defined" -
> starting IDLE and looking at sys.path confirms that the directory is there
> in the penultimate position), 

This isn't a reliable way to check sys.path for mod_python applications. 
  It's better to import sys and print sys.path to the browser. Here's a 
little module I run under mod_python.publisher sometimes to check a few 
values, including the interpreter name:

# mpinfo.py
from mod_python import apache
import os

def showformdata(req):
     formdata = {}
     if req.form.keys():
         for key in req.form.keys():
             formdata[key] = req.form[key]
         return formdata
         return "no form data"

def index(req):
     d = []
     d.extend([req.subprocess_env[i] for i in req.subprocess_env.keys()])
     for i in dir(apache):
     return "\n".join(d)

Access it at:


Point a form action at showformdata to check submitted values, if you 
need to check a form.

> and trying to import it every time I service
> a request kind of defeats the purpose, which is putting the database
> connection handler into PythonImport to avoid the overhead involved in
> it's creation at request time. As the manual says about PythonImport:
> "This is useful for initialization tasks that could be time consuming and
> should not be done at the request processing time, e.g. initializing a
> database connection."

I haven't used PythonImport for this purpose, but I get reusable db 
connections by importing them into my published modules and taking 
advantage of python's module caching. YMMV.

More information about the Mod_python mailing list