Sunday, June 26

How to run Python CGI in WAMP

I've had a few people ask me how to install Python in WAMP (Windows Apache MySQL and PHP), so here's a quick tutorial on how to set that up on your WAMP server. What I'll do is assume you already have both WAMP and Python installed, explain how to run your Python .py scripts through WAMP, then I'll have a quick tutorial explaining how to download and install both Python and WAMP individually.

Writing your first Python cgi script

Before you can run your Python scripts on the browsers, you'll need to write a .py script. Here's a quick one just to illustrate the point. Type this in Notepad and save the file as


print "Content-type: text/html"
print ""

print "Hello, World"

Now click the WAMP icon on your system tray, find the Apache option, then select the modules option. Make sure there's a check mark next to cgi_module. If there isn't a check mark next to cgi_module, go ahead and click it. This will enable the module and restart your WAMP Apache server. Now, go back to Notepad and save your file in the right cgi-bin directory. The precise path to this folder is probably different on each system, but is found inside the WAMP directory, then inside the bin directory, inside Apache. On my system, the path to the cgi-bin directory is


Once your Python script file is saved, go to your browser and access the script by going to


That's it. No need to install extra modules or a whole different server altogether. Now to a brief tutorial on installing WAMP and Python on Windows.

How to install WAMP

In this tutorial I'll assume you already have WAMP downloaded and installed. If you don't, click the link above and download the WAMP server. Installing it is really simple (click and follow the prompts), but I'll save the details to another tutorial if the need for it arrises.

How to install Python on Windows

Once you have WAMP installed, the next thing you'll need to do is to install Python. Again, this should be pretty straight forward. Click the buttons and follow the few prompts. Once the installation concludes, you should be able to open a terminal (DOS) window and verify that the installation was successful. Try typing the following command in DOS:


If you see a response that says something like Python 2.7.2 (or whatever version you installed), then the installation was successful. If this is the case, you'll see the three >>> indicating that you can start typing Python code and run it right there. If, on the other hand, you get an error message from Windows, then this means you'll need to set up a system variable with the path to your Python install. This is really simple:

How to set up Python environment variable Path on Windows

What you'll do is tell Windows to run a program called python.exe located at a certain directory whenever Windows get a request to run python. To do this, open up your Control Panel and run a search on that window for environment. Windows XP and up (Vista and Windows 7) will bring up a link that says Edit the System Environment Variables. Click that, then click the button that's labeled Environment Variables...

Next, open up a Windows Explorer window and go to the location where you have installed Python. As this could be different for different folks, you'll just have to find where you installed your own. On my home system, I installed Python at


Once you have located your main Python folder, copy the folder path and go back to the Environment Variables window. Under System variables, find the one that's labeled Path, select it, and click Edit. Now go to the Variable value field, and be sure you don't erase anything from this field. Simply go to the end of the field, place a semicolon at the end of the list, and paste the path to your main Python folder. Click OK to save this, and you're done! Now you can open up a DOS window and type in Python, and start coding to your heart's content.