Python | Posted by: Rob at 8:41 am on January 16, 2010
I’ve never really dabbled in python, so I figured I’d install it. The first thing that you need to do is download the latest version of Python here. As of January, 2010, that version would be 2.6.
It comes packaged as an executable, and you should install it. By default, a Python directory should be placed under C:\. If you installed Python 2.6, the directory will be named Python26. If this blog is old, and 2.7 or 2.8 is out, then the directory will be named Python27 or Python28 respectively.
Inside of the Python directory, there is an executable, python.exe, that you’ll need to add to your Windows PATH variable. To do this in Windows Vista, click on the start menu, right click on computer and then left click on properties.

This will bring up your system information from the control panel. Click on the Advanced system settings link in the left pane.

This will bring up a System Properties window. Click on the Advanced tab, then click on the Environment Variables button.

There should be two variable grids. The bottom grid is labeled System Variables, and the top grid is labeled User variables for {windows name here}. Directly under the top grid, click on the New button.

Fill in the Variable name textbox with PATH, and set it’s value to C:\Python26;%, or wherever python’s directory is located on your machine.

If the PATH variable has already been set, just add C:\Python26; to the beginning of the string.
Now if you open up a command prompt, and type in “python”, you should see the following response. If you don’t, try rebooting your machine. If you still don’t, you’ve done something wrong

Note: I am running an older version of python in the screen shot, that’s why it says Python 2.5.1
The next step is to configure Apache to use CGI… so open up apache’s httpd.conf file.

Make sure you have the cgi_module enabled. You can search for the following, and make sure there’s not an ; before it.
LoadModule cgi_module modules/mod_cgi.so
Scroll down to your Directory node. Mine is labeled Inside of this node, you should see something like: Options Indexes FollowSymLinks… don’t worry if yours is out of this order, or if you have more/less options. Just make sure you add ExecCGI to the end of it. So now, my has the following in it: Options Indexes FollowSymLinks ExecCGI.
Now, scroll down further, searching for the node . Inside of this node, add the following:
AddHandler cgi-script .py
Restart Apache.
Now create a test.py file with the following content:
-
-
#!C:/Python26/python.exe
-
-
print "Content-type: text/html\n"
-
-
print """
-
<html>
-
<head><title>It works</title></head>
-
<body>
-
You can now run python scripts! Yay.
-
</body>
-
</html>
-
"""
-
Try run it through your web browser and hopefully it’ll work! Note that the first line must contain
Or, whatever the path is to pythons executable.