How to install Python on XAMPP for Windows XP running Apache v2.2
and probably a bunch of other systems by making certain changes of varying degrees of intuitiveness
XAMPP is an Apache web server distribution that makes it very easy to install and run a pre-packaged system that includes Apache, PHP, a database manager, and a few other optional components. It does not, unfortunately, include Python, one of the most popular scripting languages currently in use. Google has thrown their hat in with Python, and if it's good enough for Google, it's good enough for me.
Actually installing Python is the easy part. Integrating it with Apache as a module, like mod_perl does for Perl, was a bit more troublesome.
First, we need to make sure that you can get a combination of Apache and Python that is supported by mod_wsgi. Go to http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 and see what combinations they support. I used Apache 2.2 and Python 2.7. You can see which version of Apache you are running by going to http://localhost and clicking XAMPP's phpinfo() link (apache2handler section, Apache Version). Then go to http://www.python.org/download and make sure a compatible version of Python is available. If not, I can't help you.
You may also wish to make a backup copy of XAMPP or at least your htdocs folder.
Before we begin, here's what you should not do:
- Do not install mod_python. This is an older Apache module and does not support the current versions of Python. The correct module is called mod_wsgi, after the standard it follows.
- Do not install Python 3.3. Even mod_wsgi does not support Python 3.3 (yet). Install Python 2.7 instead. This is also the version of Python that Google currently uses.
- Do not do more than one thing at a time. If Apache fails to start, you will be left wondering which thing you did last messed it up, so don't, say for example, also update Adobe Flash while you're doing this (entirely hypothetical situation, I would never do that myself). You will be configuring httpd.conf, so make a clean backup copy of it first.
- If Apache fails to start up, locate the apache_start.bat file and try starting it up from there rather than the XAMPP control panel, it may give you more informative error messages. If XAMPP complains that SQL is using port 3306, ignore that, it's a red herring and the problem is likely the last edit you made to the httpd.conf file.
- Despite what some advice on the Web will tell you, there is no need to create an .htaccess file. XAMPP does not create one by default.
Step 1: Download Python 2.7
Python version 2.7 is available from http://www.python.org/download as a Windows binary file. This is a forehead install, just keep bashing "enter" and accept all the defaults to install it in C:\Python27. The shebang line will be
Step 2: Add Python to the system path
With Python in your system path, you will be able to run Python scripts from a DOS window and double-check that it is installed correctly. This will save you a headache when trying to figure out why Apache isn't running your web scripts. Right-click My Computer and select Properties, choose the Advanced tab, and click the Environment Variables button. The System Variables window will have an entry called Path. Select it and click Edit. Paths are separated by semicolons. Add
to the end of the existing list. I'm not sure the "scripts" one is 100% necessary but the tutorial I used told me to include it and at least it didn't break anything.
Step 3: Test Python from DOS
Open a command prompt window (Windows+r, run cmd) and type python. If you get an error or "Bad command or file name" then Python is not installed properly. If you get some information, including the version of Python you have installed, it is installed properly. This also places you into the Python runtime environment. You can exit it with quit() or just close the command prompt window.
Step 4: Download mod_wsgi
The obvious module to download is mod_python, but you should not do this, it will not work. The mod_wsgi module is available from http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 and is the one you want. This is a small ".so" file with a long name identifying which combination of Apache and Python it supports. Rename it to
and put it in your Apache Modules folder presumably located at C:\xampp\apache\modules if you did a default installation.
Step 5: Modify httpd.conf
Make a clean copy of httpd.conf first! If the file you modified does not work, you will need to replace it with your clean backup copy to get Apache running again.
You can pull up this configuration text file easily from XAMPP's control panel, just click the "config" button, or you can find it at C:\xampp\apache\conf\httpd.conf if you did a default installation.
For the first edit, you will see a large section of "LoadModule" lines, some of which start with "#" characters. # identifies a comment line in the config file, any line starting with a # is ignored (for bug testing, you can comment out a line with the # character to temporarily remove it, then stop and restart Apache). Add
LoadModule wsgi_module modules/mod_wsgi.so
to the end of that list (you did rename the .so file in step 4, correct?)
For the second edit, you will need to find the line
AddHandler cgi-script .cgi .pl .asp
or similar. The exact list of file extensions will vary from one install to another. Add .py to the end of this list.
AddHandler cgi-script .cgi .pl .asp .py
Step 6: Restart Apache
From the XAMPP control panel, click the stop button and wait for Apache to shut down. Then click the start button to bring it back up. If Apache fails to restart, you almost certainly have a problem with your httpd.conf file edits. Erase the changes you made or replace the file with your clean backup copy and try to restart Apache again. If it works, you probably did something wrong with the httpd.conf edits or forgot to rename the mod_wsgi.so file or named it incorrectly.
If it doesn't work, using the apache_start.bat file may give you a more informative error message to work with. If Apache fails to restart even after you fixed the httpd.conf file, try uninstalling Python and removing the mod_wsgi.so file from the modules folder. If Apache still doesn't restart, even after a fresh reboot of the computer, I am very sorry you broke your web server and I hope you made that copy of htdocs I suggested above.
Step 7: Test Python from your browser
This is the moment of truth. Write a simple Python script by creating a text file called hello.py and putting it in one of the directories in htdocs, perhaps \htdocs\test\hello.py. Since Python is whitespace sensitive, make sure it is formatted correctly. This shebang line assumes Python in installed in C:\Python27\. Open the script with your web browser (http://localhost/test/hello.py if you used that path) and see if it works.
print "Content-type: text/html"
print "<html><head><title>Python test</title></head>"
print "<body><p>Python is working.</p></body></html>"