11.1.6 Installing your CGI script on a Unix system

Read the documentation for your HTTP server and check with your local system administrator to find the directory where CGI scripts should be installed; usually this is in a directory "cgi-bin" in the server tree.

Make sure that your script is readable and executable by ``others''; the Unix file mode should be 0755 octal (use "chmod 0755 filename"). Make sure that the first line of the script contains #! starting in column 1 followed by the pathname of the Python interpreter, for instance:

#!/usr/local/bin/python

Make sure the Python interpreter exists and is executable by ``others''.

Make sure that any files your script needs to read or write are readable or writable, respectively, by ``others'' -- their mode should be 0644 for readable and 0666 for writable. This is because, for security reasons, the HTTP server executes your script as user ``nobody'', without any special privileges. It can only read (write, execute) files that everybody can read (write, execute). The current directory at execution time is also different (it is usually the server's cgi-bin directory) and the set of environment variables is also different from what you get at login. In particular, don't count on the shell's search path for executables ($PATH) or the Python module search path ($PYTHONPATH) to be set to anything interesting.

If you need to load modules from a directory which is not on Python's default module search path, you can change the path in your script, before importing other modules, e.g.:

import sys
sys.path.insert(0, "/usr/home/joe/lib/python")
sys.path.insert(0, "/usr/local/lib/python")

(This way, the directory inserted last will be searched first!)

Instructions for non-Unix systems will vary; check your HTTP server's documentation (it will usually have a section on CGI scripts).

guido@python.org