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

Jorey Bump list at joreybump.com
Wed Sep 14 21:05:21 EDT 2005


Jim Gallacher wrote:

> In answer to my own in question, PythonImport does work and there is no 
> bug. Re-reading the docs reveals this small detail which I and others 
> may have overlooked - the context for the directive. PythonImport only 
> works in the server config context. Quoting from the apache docs 
> (http://httpd.apache.org/docs/2.0/mod/directive-dict.html#Context)
> 
> """
> server config
>     This means that the directive may be used in the server 
> configuration files (e.g., httpd.conf), but not within any <VirtualHost> 
> or <Directory> containers. It is not allowed in .htaccess files at all.
> """
> 
> I'll update the docs to make this a little more explicit, since it's 
> easy to overlook. I know I did. :)
> 
> Hope that helps.

Yes, it does. For a simple demo, put a module in the default module 
search path, such as a directory shown in sys.path when running python 
from the command line. PythonPath directives in httpd.conf will not have 
taken effect when PythonImport runs (I used 
/usr/lib/python2.4/site-packages/ for my test).

# mpglobal.py

import time

f = open('tmp/atest', 'w')
f.write(str(time.time()))
f.close()

foo = 'testing'


Then in httpd.conf (using your own interpreter name):

PythonImport mpglobal www.example.com

On Linux, restarting apache creates and/or updates /tmp/atest, verifying 
that it works.

But, like the OP, I can't figure out how to access foo (or mpglobal.foo) 
in my published module.

Also, here is a better module to run under Publisher to check various 
settings, including the name of the interpreter (and I remembered to 
include sys.path this time!):

"""
info.py

Display various settings/variables under mod_python.publisher.

Access at:

  http://host/somedir/info.py/index

or:

  http://host/somedir/info/

To check form variables, set form action to:

  http://host/somedir/info.py/showformdata

"""

from mod_python import apache
import os
import sys

topdir = []
for i in dir():
     topdir.append(i)

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

def index(req):
     d = []
     d.append("Interpreter Name (req.interpreter):" )
     d.append(req.interpreter)
     d.append("\nsys.path:")
     d.append('\n'.join(sys.path))
     d.append("\nRequest (req.the_request):")
     d.append(req.the_request)
     d.append("\nProtocol (req.protocol):")
     d.append(req.protocol)
     d.append("\nMethod (req.method):")
     d.append(req.method)
     d.append("\nreq.subprocess_env:")
     d.extend(["%s: %s" % (i, req.subprocess_env[i]) for i in 
req.subprocess_env.keys()])
     d.append("\nreq.filename:")
     d.append(req.filename)
     d.append("\nreq.path_info:")
     d.append(req.path_info)
     d.append("\nenvironment PATH:")
     d.append(os.environ['PATH'])
     d.append("\ndir():")
     d.extend(topdir)
     return "\n".join(d)



More information about the Mod_python mailing list