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 and 3.2.3 available.

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 default 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.

Our servers also have Python 3.2.3 available. 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.

Compiling a custom version of Python

It’s possible for technically advanced users to compile their own using the shell if you need a certain version of Python we don’t offer. While we can’t provide direct support for this, we’ve tested the following commands to create a working custom version of Python:

wget http://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar xzvf Python-2.7.14.tgz
cd Python-2.7.14
./configure --prefix=$HOME/private-python
make
make install

After doing this, ~/private-python/bin/python -V works as you’d expect:

$ ~/private-python/bin/python -V
Python 2.7.14