[mod_python] Module getting imported twice?

Sean Reifschneider jafo at tummy.com
Mon May 24 06:25:06 EDT 2004

I'm having a kind of weird problem and I don't fully understand why it's
happening.  Basically, it seems that sometimes a module I'm importing
(from site-packages), is losing it's mind.  It's like half way through a
function it's running in entirely a different name-space.  Yes, this can
happen while making two calls into a module on successive lines of a

What I traced it down to is that different calls are showing the
following values for the module:

   <module 'jotweb.jotwebutils' from
      '/var/www/jotweb/www.tummy.jotweb.jotwebutils' from

I really don't understand the latter.  My HTML and site-related python
files are in "/var/www/jotweb/www.tummy.com/html".

If, in jotwebutils.py, I dump the stack when the module is imported, I
see that the first time it's imported with the following:

   File "/var/tmp/mod_python-3.0.4-root/usr/lib/python2.2/site-packages/mod_python/apache.py",
      line 274, in HandlerDispatch 
   File "/var/tmp/mod_python-3.0.4-root/usr/lib/python2.2/site-packages/mod_python/apache.py",
      line 440, in import_module 
   File "lib/jotweb/__init__.py", line 32, in ? 

and the second time it's imported with:

   File "/var/tmp/mod_python-3.0.4-root/usr/lib/python2.2/site-packages/mod_python/apache.py",
      line 274, in HandlerDispatch 
   File "/usr/lib/python2.2/site-packages/jotweb/jotweb_mod_python.py",
      line 45, in handler     def handler(req): 
   [... Deep into the bowels of my handler code ...]
   File "/var/www/jotweb/www.tummy.com/html/navigation.py",
      line 3, in ?     import time, string, re, dircache, math 
   File "lib/jotweb/__init__.py", line 32, in ?

Now, it should be noted that I'm using the "imp" module to load modules
dynamically when they are referenced from my HTML templates.  But the
code in question that is triggering this is:

   from jotweb import jotwebutils
   import jotweb

   navbar = jotwebutils.getGlobal('navbar')
   urlBase = jotweb.config.getconfig().get('tummyurlbase', 'unknown')

jotwebutils.getGlobal is very simple:

   def getGlobal(name): 
      from jotweb import config

If I change the "tummyurlbase" line above to:

   urlBase = jotwebutils.getGlobal('jotwebconfig').get('tummyurlbase', 'unknown')

it works fine.  I can do many "getGlobal()" lines, but if I do a
"getconfig()", it's suddenly using another jotwebutils module.

Note that all the jotweb imports are done using "import".  Only local
files in my HTML directory are imported using the "imp" module.

I'm able to find workarounds for these issues in most cases, but it's
kind of driving me crazy as far as when something will work and when it
will lose it's mind.

I tried moving getconfig() into the jotwebutils module, but that just
seemed to make it worse.  Then getGlobal() was also acting this way.

Any thoughts?

 How does a girl like you get to be a girl like you?
                 -- _North_by_Northwest_
Sean Reifschneider, Member of Technical Staff <jafo at tummy.com>
tummy.com, ltd. - Linux Consulting since 1995.  Qmail, Python, SysAdmin

More information about the Mod_python mailing list