"No module named Image" CGI error

Posted on 2006-04-22
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 ="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                                              , "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
    LVL 15

    Expert Comment

    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

    Of course, I meant "The PYTHONPATH environment variable tells Python where to look for modules."
    LVL 2

    Author Comment


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

    ['(\\.py$)/', '/usr/lib/', '/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

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    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…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now