Rodrixar

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 hello.py:

#!python

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 hello.py 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

C:\wamp\bin\apache\Apache2.2.17\cgi-bin


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

http://localhost/cgi-bin/hello.py



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:

python


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

C:\Python


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.

13 comments:

  1. it gives error

    You don't have permission to access /python/hello.py on this server.

    ReplyDelete
    Replies
    1. same problem occured with me...please help

      Delete
  2. This was great info in an easy to understand and very short format. Just what I was looking for (which is rare, lol), thanks!

    ReplyDelete
  3. This is ridiculously vague

    ReplyDelete
  4. http://localhost/cgi-bin/hello.py gives 500 internal server error

    ReplyDelete
    Replies
    1. Error 500 is a server error. In this case most likely caused by a bad python script.

      Delete
    2. I get the same error after following your guide.

      Delete
  5. THANKS! This with environment variable was the part that the first guide I was following had left out. Now python is up and running on my wamp. <3

    ReplyDelete
  6. worked well for me, just needed to make sure i checked everything out when installing both WAMP and python and python addon.

    i started with wamp. used

    http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/ to test php, that works. I tested the hello.php in the www file in wamp C:\wamp\www

    that worked.then i installed python. i used the x86(32bit) version with THE MATCHING python addon from https://github.com/WampServerAddons/python-addon

    then after setting u enviormental variables and configuing apaches config file httpd.conf i restarted the server, and wamp when server was up.

    now in cmd, type python and if it doesnt display python info, somethings wrong...fix it now!

    i played with env variables to add: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\wamp\bin\python\Python27

    then it worked.

    then i tested hello.py with the following saved to notepad as hello.py saved too the www folder in wamp

    then it returned the "Hello World" as expected.

    read http://docs.python.org/2/using/windows.html

    hope this helps....

    ReplyDelete
  7. hi.

    does not work for me..using python 3.3.2...

    using the link gives internal servor error;

    cgi module is already checked

    ReplyDelete
  8. Bonjour,

    J'ai suivi votre démarche mais il m'affiche il m'affiche le messge NotFound
    The requested URL /input_query.py was not found on this server.

    j'ai insallé wampserver (apache 2.4.4) et python 3.3.2

    ReplyDelete
  9. je vous remercie d'avance

    ReplyDelete