"No module named Image" CGI error

Hi there,

I'm trying to get some code going on my website to dynamically generate images with Python.

I have SSHed into my server and when I run Python from the command line I get no problems at all when I do:

"import Image"

However, the following code:

#!/usr/bin/env python

print "Content-type: image/png"
print "Status: 200 Ok"

import Image, ImageDraw

im = Image.new("RGB", (40, 15), 'white')
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill="red")
draw.line((0, im.size[1], im.size[0], 0), fill="red")
del draw                                                      

im.save(sys.stdout, "PNG")

gives an error 500, and in my logs shows up as:
[Sun Apr 23 13:30:10 2006] [error] [client x.x.x.x] PythonHandler mod_python.cgihandler: ImportError: No module named Image

Any ideas what I'm doing wrong?


Who is Participating?
PAQed with points refunded (250)

Community Support Moderator
The PYTHONPATH environment variable tells Python where to look for libraries.  Probably when the web server runs your script, PYTHONPATH doesn't have the same value as it does when you run the script from the command line.  You can check this by writing a CGI script that displays the contents of this variable to the browser.

There are lots of ways you could fix it; unfortunately, I don't know what is standard.  You can customize the Python installation so it always looks in the directory where the Image module is, or you could possibly configure the web server so that when it runs the script, it has the right directory in its PYTHONPATH variable.  Or you could change the module search path within your script.  Or you could put the Image module in the same directory as the CGI script.  Or possibly you could set the PYTHONPATH in the shell initialization script for the user identity the web server uses to run the script.

I think the way I have avoided this problem in my limited Python CGI experience is to have all the non-built-in modules in the CGI directory.
Of course, I meant "The PYTHONPATH environment variable tells Python where to look for modules."
SammoAuthor Commented:

I did a bit of digging around, I printed out my pythonpath variable and it seemed correct as it was:

['(\\.py$)/', '/usr/lib/python23.zip', '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2', '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload', '/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/PIL', '/usr/lib/python2.3/site-packages/gtk-2.0']

You can see that PIL is already there.

I did manage to solve the problem though, it seems that Image wouldn't import correctly unless I imported the sys and os modules first. I've written a lot of python but this is the first time I've dabbled with CGI.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.