[mod_python] ImportError: No module named index

Graham Dumpleton grahamd at dscpl.com.au
Thu Oct 21 21:42:50 EDT 2004

On 22/10/2004, at 11:22 AM, Robert Geller wrote:
> It returns the following error:
> Mod_python error: "PythonHandler index.n::index"
> Traceback (most recent call last):
>   File 
> "/usr/local/python/lib/python2.4/site-packages/mod_python/apache.py", 
> line 287, in HandlerDispatch
>     log=debug)
>   File 
> "/usr/local/python/lib/python2.4/site-packages/mod_python/apache.py", 
> line 454, in import_module
>     f, p, d = imp.find_module(parts[i], path)
> ImportError: No module named index
> Oddly, when I make the handler file, or module, "index.py", it works! 
> But I don't *want* to make any related file have the
> *.py extension, even if it's just the handler. I realize I can use 
> index.py for the handler and then *.n for the front-end files,
> but that's not how I want to do things.
> If anybody could please advise me on making actual *handlers* without 
> .py extensions, that would be great!

Using mod_python by itself it cannot be done. This is because the
mod_python.apache.import_module() uses the Python "imp" module
and it will only allow importing of modules with a valid Python
module extension.

   >>> import imp
   >>> imp.get_suffixes()
   [('.so', 'rb', 3), ('module.so', 'rb', 3), ('.py', 'U', 1), ('.pyc', 
'rb', 2)]

The mod_python.servlet system uses an alternate extensions to .py,
namely .mps, but it only manages this by not using the Python "imp"
module and instead using exec/execfile() to implement its own module
loading system. When using exec/execfile() you can give it any
arbitrary filename and it will read its contents as Python code.

Now, if the reason you don't want to use the .py extension is purely
so that the means of implementation isn't reflected on the URL, then
have a look at Vampire (http://www.dscpl.com.au/projects/vampire).
This allows you to use more traditional file extensions appropriate
to the type of content being returned, but even though access to the
actual Python code file is blocked, the Python code is still stored
in a .py file. Vampire is though implemented in a way to avoid a
lot of the problems which arise with using a .py extension in the
first place.

As to what else you can do, you would have to explain better why
you don't want to use a .py extension for the Python code files.

Graham Dumpleton (grahamd at dscpl.com.au)

More information about the Mod_python mailing list