[mod_python] ImportError: No module named _apache

Graham Dumpleton grahamd at dscpl.com.au
Mon Nov 22 16:22:05 EST 2004

On Nov 22 13:30, Alan Ezust <alan.ezust at gmail.com> wrote:
> Subject: Re: [mod_python] ImportError: No module named _apache
> I am getting the same error message. I am using the pre-installed
> apache version 1, and I apt-got apache-mod-python.
> my apache log looks like this:
> Traceback (most recent call last):
>   File "/home/ezust/alan/school/uvic/phd/distquery/bin/modtest.py", line 3, in ?
>     from mod_python import apache
>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 55, in ?
>     import _apache
> ImportError: No module named _apache
> Which package is the module _apache supposed to come from?

It is installed by the mod_python module when it is loaded by Apache at startup.
Thus, only scripts which are executing under mod_python will actually be able
to import it.

The _apache module will not be available to a Python script which is executed
by Apache as a CGI script.

I raise this specific issue as this is the main reason one can see this error.
Specifically, that the Apache configuration is such that your script is actually
getting executed as a CGI script and not as a mod_python content handler.

One can generally tell this by looking at the messages in the Apache error log
file. You list some of the messages from your error log, but what comes just
after that. Do you also get an error messages talking about "Premature end
of script headers" as shown below.

Traceback (most recent call last):  
  File "/usr/local/etc/httpd/cgi-bin/cgi-env.cgi", line 3, in ?
    from mod_python import apache
  File "/usr/local/lib/python2.3/site-packages/mod_python/apache.py", line 55, i
n ?
    import _apache
ImportError: No module named _apache
[Mon Nov 22 14:09:52 2004] [error] [client] Premature end of scri
pt headers: /usr/local/etc/httpd/cgi-bin/cgi-env.cgi

This is what I get if I try and import mod_python.apache into a Python CGI script.

> David, I am running the latest version of Debian on my computer.  Also there
> is no error when compiling the _apache module into mod_python and that is
> what my original concern was that there seems to be no exception raised if
> the compilation fails for _apache.
> Here is what I am talking about:
> _apacheModule.c: void init_apache(): line 533
> Mp_ServerReturn = PyErr_NewException("_apache.SERVER_RETURN", NULL, NULL);
> if (Mp_ServerReturn = NULL)
> 	return;
> _apacheModule does not seem to be raising any exceptions when that occurs.

I wouldn't be too concerned about that as it is unlikely to raise an exception as
it is compiled C code which is a part of mod_python itself.

> Brian, I just signed up with the list before I posted this message.  How can
> I check all the previous list entries to find out how this problem was
> covered.

FAQ: http://www.modpython.org/FAQ/faqw.py
ML ARCHIVE: http://www.modpython.org/pipermail/mod_python/

> As far as not running this through mod_python on the apache server, as far
> as I am concerned, I am.  I pretty much followed exactly what is written in
> the documentation including the test file they have to test mod_python, in
> which the call to import apache fails:
> from mod_python import apache
> Any ideas guys?

Ensure that you haven't put your Python code file in a directory which has some
sort of overriding directive that says that all stuff there should be executed as a
CGI script. Alternatively make sure there is no mapping in place which marks a
.py file as a CGI script.

You might also post how you have set up you httpd.conf and/or .htaccess file
and the contents of your Python code file you are using to test so it can be
confirmed that it is correct.

Although the documentation seems to emphasise enabling mod_python for a
directory from the httpd.conf file, this seems to cause more problems that good.
Try instead doing it from a .htaccess file if you are doing it from httpd.conf. At
least then you don't usually have to restart Apache to see if it is going to work.

Graham Dumpleton (grahamd at dscpl.com.au)

More information about the Mod_python mailing list