[mod_python] mod_python weirdness, maybe related to importing?

David Fraser davidf at sjsoft.com
Tue Jun 10 09:12:46 EST 2003

Sean Reifschneider wrote:

>I've been trying to build a web application system using mod_python, and
>I've been running into some weirdness.  Some of it is just sporadic
>behavior, some of it seems to be pretty consistent.  Before I get
>started, my main development system is running Red Hat 9 with Python
>2.2.2 and I've been trying mod_python 3.0.1 (shipped with RH9) and
>Basically, I'm building up a package that contains my system.  In it I
>have a module which contains my handler:
>   AddHandler python-program .html
>   PythonHandler jotweb.jotweb_mod_python::handler
>Basically, my handler is using SimpleTAL to parse some HTML, which will
>result in my code getting called, which will import things from the
>jotweb package:
>   fileName = path + '.py'
>   fp = open(fileName, 'r')
>   module = imp.load_source(path, fileName, fp)
>So, basically, I am trying to load a module from a specific file.
Do you mean you're using this import mechanism when you have the problem 
with jotweb.input.Request below?

>The first problem I'm seeing is that imports are just weird.  For
>example, if I have code which does "import jotweb.input.Request",
>unless my __init__.py in jotweb/input includes:
>   import Request
Have you tried using __all__ = [Request, ...] in __init__.py ?

>I will get an error to the effect that the module object contains no
>element "Request".  Of course, from a regular Python program, I can do
>"import jotweb.input.Request" just fine either way.
>Basically, because of the above, my entire code-base has to be imported
>when you do an "import jotweb".
mod_python does have some import hooks to handle things specially, but 
I'm not an expect here, someone else will have to comment...

>Further, I'm running into weird issues with headers_out.  If from my
>jotweb_mod_python.handler code I do:
>   req.headers_out.add('key', 'value')
>it will properly add the specified header.  And if this code is executed
>from the module that is imported as above, it seems to work.  If,
>however, in this imported module I do:
>   import jotweb
>   jotweb.auth.Sessions.processLogin()
>where "processLogin()" gets a handle to the request object and does
>req.headers_out.add(), the headers aren't showing up in my browser.
>HOWEVER, if I telnet to port 80 and issue a GET request to exactly the
>same URL that I issued in the GET request, the headers are *NOT* getting
>return as far as I can tell (no cookie is getting set, doing "View
>Documenat Information" shows not those headers, but if I set headers in
>my main mod_python handler, it WILL show it.
>I've verified that the call to headers_out.add() is happening by adding
>a syslog before it, it's definitely calling that code.
How do you pass the request object into processLogin (it doesn't seem to 
be part of the call)?
It seems like you may be trying to use an old request object or something...

>It seems like at some point something is making it so that
>headers_out.add() doesn't work any more.  Would pulling the form data
>out of the request cause that?  Any thoughts on why the imports seem to
>work quite differently on a name-space basis from how they happen from
>the interpreter:
Pulling the form data won't cause it, but writing anything to the 
request will (it automatically outputs the headers as soon as any 
content is written).

>   AttributeError: 'module' object has no attribute 'input'
>Is there something about being in mod_python that makes it a "toy"
>interpreter in some ways, that mean I'm constantly going to be bumping
>into weird things?
No. But it is a bit more complex running a python interpreter inside 
Apache than standalone.

Hope that helps,


More information about the Mod_python mailing list