[mod_python] __import__ error

Richard Lewis richardlewis at fastmail.co.uk
Mon Jun 12 07:15:41 EDT 2006

Hello mod_pythoners,

I'm still working on that code to write a little Cocoon replacement which I 
posted regarding the other week. Its going quite well, but I've come up 
against another problem that I can't seem to find the answer to:

OK, this works:
_>>> module = "transforms.search_filter"
_>>> handler = "search_filter"
_>>> import os
_>>> os.chdir("/var/www-studio/")
_>>> sf = __import__(module.replace(".", os.sep)).__dict__[handler]()
_>>> dir(sf)
[..expected output...]

it corretly imports the module "/var/www-studio/transforms/search_filter.py" 
and creates an instance of its "search_filter" class.

However, the same code used in my application throws the following ImportError 
File "/usr/local/lib/python2.3/site-packages/pycoon/transformers/sax_handler_transformer.py", 
line 30, in __init__
    self.handler = __import__(module.replace(".", os.sep)).__dict__[handler]()
ImportError: No module named transforms/search_filter

I've tried giving it absolute paths, mixing various combinations of "." and 
os.sep and also using the imp.find_module and imp.load_module functions but I 
still get the same problem. (Actually, the imp.* functions tell me that there 
is no frozen module or some such bizarre error...)

As it works from the interpreter, I can only assume that it must be something 
to do with my application. However, I've been constantly re-designing as I 
work (one of the joys of Python!) and this also used to work in my 
application when I was using one *big* script file which lived in the same 
directory as "transforms/search_filter.py". I've now separated most of the 
classes out into their own files and split the "library"-like code away from 
the "program"-like code like this:

The "library"-like code:
  __init__.py    # the handler(req) function is implemented here
    sax_handler_transformer.py   # this is where the problem is

the sax_handler_transformer.py module contains a sax_handler_transformer class 
whose __init__ method tries to import a variable named 
xml.sax.handler.ContentHandler class like this:

def __init__(self, module, handler):
  self.handler = __import__(module.replace(".", os.sep)).__dict__[handler]()

Then the website which is using the modules (the "program"-like code) is like 

  sitemap.xml # Specialised config file
  studio.conf   # Apache config: PythonHandler pycoon
    search_filter.py   # this is where the problem is,
                              # contains search_filer class
                              # derived from xml.sax.handler.ContentHandler

I'm using mod_python, but I also get the same error when I use my command line 
testing script. Its Python 2.3.5 as supplied by Debian "unstable".

Um, I hope that makes sense. Any thoughts on why this isn't working would be 
very much appreciated.

Richard Lewis
Sonic Arts Research Archive

More information about the Mod_python mailing list