Graham Dumpleton
grahamd at dscpl.com.au
Mon Jun 12 19:39:10 EDT 2006
On 12/06/2006, at 9:15 PM, Richard Lewis wrote: > 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/") Don't use os.chdir() in mod_python or rely on what the current working directory may be as there can be threaded MPMs for Apache and thus each request thread can fight each other wanting to change the current working directory thereby screwing each other up. > _>>> sf = __import__(module.replace(".", os.sep)).__dict__[handler]() Note that using 'os.sep' in module naming hierarchy only works on some platforms when using __import__ and thus is not portable. It may work on Linux, but doesn't work on Mac OS X. Don't know if it works on Win32 or not. > _>>> 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 > exception: > > 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...) When import in mod_python is done, it will not necessarily look in current working directory anyway. You need to add directory to Python PATH explicitly using the directive: PythonPath 'sys.path+["/var/www-studio/"]' in your Apache configuration for where you are using mod_python. Graham > 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: > /usr/local/lib/python2.3/site-packages/pycoon/ > __init__.py # the handler(req) function is implemented here > /sources/ > __init__.py > xml_source.py > ... > /transformers/ > __init__.py > xslt_transformer.py > 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 > this: > > /var/www-studio/ > sitemap.xml # Specialised config file > studio.conf # Apache config: PythonHandler pycoon > transforms/ > site2html.xslt > 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. > > Cheers, > Richard > -- > -=-=-=-=-=-=-=-=-=-=-=-=-=- > Richard Lewis > Sonic Arts Research Archive > http://www.sara.uea.ac.uk/ > -=-=-=-=-=-=-=-=-=-=-=-=-=- > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python
|