"No module named Image" CGI error

Posted on 2006-04-22
Medium Priority
Last Modified: 2012-06-27
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?


Question by:Sammo
  • 2
LVL 15

Expert Comment

ID: 16517829
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.
LVL 15

Expert Comment

ID: 16517833
Of course, I meant "The PYTHONPATH environment variable tells Python where to look for modules."

Author Comment

ID: 16518696

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.

Accepted Solution

GranMod earned 0 total points
ID: 16727419
PAQed with points refunded (250)

Community Support Moderator

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question