[mod_python] erratic import errors with mod_python

Jim Gallacher jpg at jgassociates.ca
Sun Jun 4 10:18:53 EDT 2006


Detmar Meurers wrote:
> Hi,
> 
> I'm using mod_python's publisher handler to load a .py file using
> the usual
> 
> AddHandler python-program .py
> PythonHandler mod_python.publisher
> PythonDebug On
> 
> .htaccess specification in the directory in which the .py file
> resides. That .py file imports some other modules, both standard ones
> and some auxiliary functions I wrote and put into a separate file.
> 
> Usually the code executes fine, but every five or so times the page
> is loaded (without changing anything, e.g. just doing several
> reloads in a row), I get the error that mod_python for some reason
> cannot find one of the modules that is being imported (namely the
> file with the auxiliary functions, called tagarela_util.py, which is
> in the same directory). I'm including an example error message
> below.
> 
> This error never seems to appear for the standard modules I import,
> (or when I put that file into the python2.4/site-packages which is 
> in sys.path), so I suspect that it has something to do with the way
> mod_python seems to use the .htaccess files to determine which
> directory to add to the sys.path?
> 
> I very much hope someone can explain this erratic behavior and how I
> can fix it - as it stands the only way I found to avoid these
> occasional errors arising when importing modules is to put
> everything into the same file, i.e. to avoid importing modules,
> which naturally leads to massive code duplication and
> non-modularity.
> 
> Any suggestions would be much appreciated!
> 
> Best,
> Detmar
> 
> --
> Detmar Meurers, Associate Professor, Dept. of Linguistics, OSU
> 201a Oxley Hall, 1712 Neil Avenue, Columbus OH 43210-1298, USA
> http://ling.osu.edu/~dm/                 GnuPG key on web page
> 
> ****Server used:
> 
> 'SERVER_SOFTWARE': 'Apache/2.2.0 (Unix) mod_ssl/2.2.0 OpenSSL/0.9.8a DAV/2 mod_python/3.2.8 Python/2.4.2'
> 
> (Errors appears with a variety of browsers, so seems to be independent
> of that.) 
> 
> ****Error message:
> 
> Mod_python error: "PythonHandler mod_python.publisher"
> 
> Traceback (most recent call last):
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
>     result = object(req)
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/publisher.py", line 204, in handler
>     module = page_cache[req]
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/cache.py", line 82, in __getitem__
>     return self._checkitem(name)[2]
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/cache.py", line 124, in _checkitem
>     value = self.build(key, name, opened, entry)
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/publisher.py", line 77, in build
>     return ModuleCache.build(self, key, req, opened, entry)
> 
>   File "/opt/local/lib/python2.4/site-packages/mod_python/cache.py", line 371, in build
>     exec opened in module.__dict__
> 
>   File "/home/dm/public_html/icall/tagarela/activities/Reading/index.py", line 3, in ?
>     from tagarela_util import _get_session, _file2string
> 
> ImportError: No module named tagarela_util

Is it possible that targarel_util is being imported by the standard 
python import statement, as well as by publisher? The importer is being 
revamped for 3.3 and will hopefully take care of this sort of problem, 
but I think the best plan is to move your util files outside of the 
document tree. Use PythonPath "['/path/to/your/utils'] + sys.path" and a 
standard "import targarela_util". The drawback here is that these 
modules won't get re-imported if they change, necessitating an Apache 
restart. In practice I don't find this to be a huge burden since my 
support files seldom change on the production machine.

Jim





More information about the Mod_python mailing list