Importing pylab in python CGI script causes immediate failure

Hi,

I’m having a problem when importing pylab in a python CGI script to generate some dynamic graphs for a web page.

As soon as I import pylab, my script will fail, and I have no idea why.

As an example, i can have 1x1 black png file, loaded from a script like thus:

#!/usr/bin/env python

black.py

fh = open(‘black.png’)
print “Content-type: image/png\n”
print fh.read()

And called from a HTML page via:

A black dot

This will work perfectly until i modify the script to:

#!/usr/bin/env python

black.py

import pylab
fh = open(‘black.png’)
print “Content-type: image/png\n”
print fh.read()

And I have no idea why :frowning:
I’m fairly new to HTML, and I can’t seem to see any meaningful errors in Firebug. Anyone have any experience with an error like this?

The script runs fine from a terminal and outputs as expected.
I’m using python2.4, python-matplotlib-0.91.2-1.el5.rf on Centos 5.3 (old I know, but this is a production server)

Any help would be much appreciated.

Hello Mark,

···

On Tue, Jun 1, 2010 at 08:17, Mark Boorer <markboo99@...287...> wrote:

And I have no idea why :frowning:
I'm fairly new to HTML, and I can't seem to see any meaningful errors in
Firebug. Anyone have any experience with an error like this?

This is not HTML, it's a CGi, so a script executed server-side; to
know what's going wrong, check the log files of your web server.

Regards,
--
Sandro Tosi (aka morph, morpheus, matrixhasu)
My website: http://matrixhasu.altervista.org/
Me at Debian: http://wiki.debian.org/SandroTosi

Hi Sandro,

Thanks for the tip.

In checking the HTTP logs, I found that the apache user couldn’t create a “.matplotlib” directory (as it had no home dir) so i used the environment variable MPLCONFIGDIR to point to somewhere meaningful, then I found that to “import pylab” I needed a DISPLAY variable etc, etc, and I’ve eventually got everything working as expected by importing like thus:

import os
os.environ[‘MPLCONFIGDIR’]=‘/path/for/.matplotlib’
import matplotlib
matplotlib.use(‘Agg’)
import matplotlib.pyplot as plt

Found this too, I should have RTFM :smiley:
http://matplotlib.sourceforge.net/faq/howto_faq.html#howto-webapp

Cheers,
Mark

···

On Tue, Jun 1, 2010 at 4:44 PM, Sandro Tosi <morph@…10…> wrote:

Hello Mark,

On Tue, Jun 1, 2010 at 08:17, Mark Boorer <markboo99@…287…> wrote:

And I have no idea why :frowning:

I’m fairly new to HTML, and I can’t seem to see any meaningful errors in

Firebug. Anyone have any experience with an error like this?

This is not HTML, it’s a CGi, so a script executed server-side; to

know what’s going wrong, check the log files of your web server.

Regards,

Sandro Tosi (aka morph, morpheus, matrixhasu)

My website: http://matrixhasu.altervista.org/

Me at Debian: http://wiki.debian.org/SandroTosi