[mod_python] Accessing objects loaded with PythonImport

Lee Brown administrator at leebrown.org
Sat Jun 4 16:04:59 EDT 2005


 Greetings!

First, thanks to all who sent replies; they were very helpful.  I have
solved the problem and I am posting it here for future reference:

My first step was to ensure that site_setup.py was actually executing at
server initialization, so I changed it to this:

	file = open('junkspace.txt', 'w')
	file.write("It's magic, I tell you!")
	file.close()
	fetch = open('junkspace.txt', 'r')
	magic_string = fetch.read()
	fetch.close()

By watching the file timestamp, I would be able to see when 'junkspace.txt'
was being touched.

But 'junkspace'txt' never appeared.  So I tried adding this to
xmlhandler.py:

	import site_config.py

Now 'junkspace.txt' was being created, but it updated only when
xmlhandler.py was being called by a request.  Not the results I was looking
for; the context of site_config was within xmltest.py and not within the
subinterpreter as was intended.  So I went back and very carefully checked
the mod_python docs, sections 4.1 and 5.4; and the python C API, section 8.
It turns out that I had made a mistake in my initial setup:  My PythonImport
statement was not in the 'main' section of httpd.conf as I originally
thought; it inadvertantly ended up in a 'vhosts' section.  So I made these
changes:

In httpd.conf 'main'

	...
	AddHandler mod_python .py
	PythonPath ...(snipped for brevity)...
	PythonImport site_setup.py crashtest
	...

And then in my vhosts configuration:

	<VirtualHost *:80>
		ServerName crashtest.leebrown.org
		...
		PythonInterpreter crashtest
		PythonDebug On
		...

Poof!  Now 'junkspace.txt' updates only on server startup and 'from
site_setup import magic_string' returns the correct results within the
context of the handler.

For reference, I found the documentation for the Py_NewInterpreter function
to be most useful.  By examining the contents of sys.modules I was able to
verify which objects were showing up in which namespaces - very handy, and
since sys.modules is always globally available within an interpreter. It can
be inspected even if other parts of your code are falling apart like a clown
car at the circus.

So it turns out that the answer was indeed 'dirt simple:' check your setup
carefully, and read the documentation thoroughly!

Best Regards,
Lee E. Brown
(administrator at leebrown.org)




More information about the Mod_python mailing list