How do I use Python scripts?

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

Our Web hosting service allows you to use CGI scripts created in the Python programming language. We currently have Python versions 2.7.3, 2.6.8 and 3.2.3 available (although the 2.6 series will soon be removed from our servers).

On this page:

Use the correct path

First of all, make sure that the first line of your script points to the correct location of Python on our servers.

If you expect your script to be compatible with any version of Python, you should use this path:

  #!/usr/bin/python

This is the default “path” to the recommended Python version on our servers. That’s currently version 2.7.3, but will be updated from time to time to match the Debian Linux stable version.

If, on the other hand, you need to make sure your script uses a certain major version of Python even when the server version is updated, you can use this path to explicitly specify the Python 2.7 series:

  #!/usr/bin/python2.7

Or this path to specify the obsolete Python 2.6 series:

  #!/usr/bin/python2.6

If you use the obsolete path, you should immediately work to update your Python scripts for at least version 2.7.3 compatibility, because the Python 2.6 series will soon be removed from our servers.

Our servers also have Python 3.2.3 available, although it’s also officially unsupported and experimental for now. You can access it as:

  #!/usr/bin/python3

Publish using text (ASCII) mode in your FTP program

Use your FTP program to upload the script file in text mode (sometimes called "ASCII mode"), not "binary" mode. Although text mode is the default for most FTP programs, your Python script may not work if it's accidentally uploaded using binary mode, so it's wise to check.

Put the script in the cgi-bin directory

When you upload the script, place it in a directory (folder) named cgi-bin using your FTP program. You will need to create this directory the first time you upload a script.

(Advanced users can make other directories run scripts like the "cgi-bin" directory; see our "Making Additional Directories Executable" page for more information.)

Make the script executable

Set the file permissions to make your script "executable" after uploading (you'll sometimes see this process referred to as making the script permissions "mode 0755" or "mode 0700").

Your FTP program documentation should explain how to create directories, transfer a file in ASCII mode, and change the permissions of a file you've uploaded.

Use the Script Checker

You can use our Script Checker to verify that the script was uploaded properly. The page also gives you a quick way to make the file executable if you prefer to do that on the Web, rather than using your FTP program.

Test the script

Once your Python script is installed, you'll access it as:

http://www.example.com/cgi-bin/script.py

... where "script.py" is the actual name of your script.

(Sometimes Python will automatically create a matching script.pyc" file, which it can use to make the script run faster. However, you'll still access your script using "script.py" — don't use "script.pyc" in URLs.)

Help! I see an "Internal Server Error"!

An "internal server error" means your script somehow isn't sending normal output to the Web server. The first thing to do is use our CGI Script Checker, which will verify that the script has the correct path and that it was uploaded in text mode. Always try the Script Checker first if you have trouble.

If the Script Checker says the script is okay, there is probably a bug or "typo" in the script you have uploaded. You should change your script slightly so that it displays the true cause of the error in your Web browser.

To do that, just add these two lines to your Python script, right below the "#!/usr/bin/python" line:

import cgitb
cgitb.enable()

Making this change will display the Python errors in your Web browser when you try to run the script. You should remove these lines once you've solved the problem, as they can potentially reveal parts of your script's source code to your visitors.