"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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
PAQed with points refunded (250)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.