So you want to get Python working with XAMPP eh? Me too! So, apparently, do a lot of people; and pretty much no one who knows how is saying how from what I’ve found online in my cursory 5 minute search. Let’s just jump right in, shall we?
Resources:
Step 0: Install XAMPP & Python
You should have installed XAMPP already, but if you are a brand new “b” to the Server Side Dev ring, and you have stumbled upon this post looking for the catch all solution to getting your feet wet with Server Side Programming and have picked Python as your poison, go get XAMPP! It basically installs and configures itself, and there is an excellent tutorial on the apachefriends website to get you through this process:
http://www.apachefriends.org/en/xampp-windows.html#641
You might not have installed Python already (although, if not, why did you choose Python over PHP or Ruby?). If you haven’t installed it already, go download and install it now:
http://www.python.org/download/
Step 1: Make a Backup
If you are like me, and have a few years worth of stuff in your XAMPP directory that you don’t want to ‘splode when you screw up the first time you try to get Python working with XAMPP, go ahead and just zip c:\xampp to x:\xampp-backup.zip now…
Step 2: Download mod_python
Download mod_python now…
http://httpd.apache.org/modules/python-download.cgi
Wait, which one do I download?
Good question! Check your version of Apache and Python to determine which one you need
- Enter the text (leave out the space between phpinfo and ()):
<? phpinfo (); ?>
into a new text document in notepad. - Save the document as “test.php” (or something) in the htdocs directory of your xampp install.
- Start Apache (open the xampp control panel, c:\xampp\xampp-control.exe, and click “Start” next to Apache).
NOTE: If you have IIS running, stop it. (Control Panel, Administrative Tools, Services, IIS Admin, Stop (say yes to stopping those other services too). If you scroll up to “Apache” at this point and click on it, you will see which version you are running and can skip to the next step in the mod_python install). - Browse to the document you saved in step 2 through your web browser (localhost\test.php). You can NOT just double click the file and expect it to be parsed by apache and run…
- Scroll down and view the output in the “apache2handler” section. The first box in the table should be “Apache Version” and should tell you which version of Apache you are running (mine is 2.2.4, so I downloaded the very last “.exe” file so that I would have Python 2.5 w/ Apache 2.2)
Step 3: install mod_python
- Double click the exe file
- Click “Next”
- It should find your Python install. If you have more than one version, select the one you want to use. Click “Next”
- Click “Next”
- There should be a pop-up asking where Apache is installed. If you installed xampp to c:\, then this will be c:\xampp\apache
Step 4: Configure Apache
- Open c:\xampp\apache\conf\httpd.conf
- Scroll down to the section with all the “LoadModule” lines (about line #67) and add this line:
LoadModule python_module modules/mod_python.so
Note: If you are unable to start Apache after this, go back and type the line in the conf file by hand instead of copying/pasting.
- Scroll down to the section with the <Directory “/xampp/htdocs”> tag ends (about line #232) and add these lines following the closing </Directory> tag:
<Directory "/xampp/htdocs/test"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory> - Save and close the file.
- Restart Apache
- Create a new file (and folder) c:\xampp\htdocs\test\mptest.py
- Place the following text in the file, and save it (watch your formatting, Python is whitespace sensitive!):
from mod_python import apache def handler(req): req.content_type = 'text/plain' req.write("Hello World!") return apache.OK
Step 5: Testing Py!
Point your browser to http://localhost/test/mptest.py
You should see “Hello World!”. If you don’t, check this page out (and this one and this one). Also, make sure you got the path correct in the <Directory /> tag, and saved your mptest.py file to the right place.
What’s next? Why not get a Python book? This one is really good:
Game Programming: The L Line, The Express Line to Learning (The L Line: The Express Line To Learning). That’s right! Game programming in Python.
Or, why not check out some tutorials, starting with this one! (and check out this presentation on django)
Edit:
Some user comments:
<Directory /test> … </Directory> <– This is wrong!
But, the following works:
<Directory “C:/xampp/htdocs/test”> … </Directory>
instead on adding the directory block, which turns on mod-python for a single folder, add the following at the end of the file (assumes you want to use the Publisher handler):
<IfModule python_module>
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</IfModule>
If you have any other comments that require code. Just send me an e-mail (cmcculloh-at-gmail-dot-com) and I’ll post the code (so you don’t have to worry about it getting mangled).










Great instructions, except that I believe the directory should be “htdocs”, not “dtdocs”, in the following path:
c:\xampp\dtdocs\test\mptest.py
I tried making a “dtdocs” at first, and it didn’t work. I didn’t know enough about Apache to recognize this as a typo, so it took me a couple minutes.
Other than that, though, this was a great help. Thanks!
Thanks for this! I’ve been wanting to learn Python and start coding a site with Django. I had all sorts of problems using cygwin. It seemed to work ok, except sqlite gave me problems (and it seems to be a real pain to do anything with mysql in cygwin). But these instructions got me going with apache and mod_python. Now on to the Django setup!
Thanks, your instructions have saved me some time!
A couple of additional tips. In Step 4, point 2, make sure you can start Apache at that point. If you cannot, as suggested try typing the line rather than copy/pasting (or use the ‘view all characters’ on an editor such as Notepad++ to make sure no odd characters have crept in!).
If it still won’t work, make sure Python is in your system PATH. It isn’t installed by default.
Finally, instead on adding the directory block, which turns on mod-python for a single folder, add the following at the end of the file (assumes you want to use the Publisher handler):
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
My last comment got mangled – if should read:
%IfModule python_module%
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
%/IfModule%
Replace % with as appropriate!
Arrgh still mangled – should read replace % with ‘less than sign’ or ‘greater than sign’ as appropriate.
For those of you who have looked thru all forums out there and still haven’t managed to figure out what’s wrong, check to see if you’ve made the same noob mistakes that I had:
1. I was trying to be smart and had only the relative path
2. I had left out the quotes in the tags.
i.e. I had … #<— This is wrong!”
… #<— This works!”
I’m not completely sure if it was using the absolute path or using the quotes, or both, that fixed the problem, but make sure to check how you have your conf file saved.
replace % % with the appropriate angle brackets.
i.e. %Directory /test% … %/Directory% #<– This is wrong!
But, the following works:
%Directory “C:/xampp/htdocs/test”% … %/Directory%
First of all, thanks a lot to Christopher for making this article. It got me through the first stage.
@Paul: I needed the capability to run .py files from any location. Thanks for your addition.
However, I found mptest.py was not showing proper output when I used ‘publisher’. Google tells, this is how I should write the mptest.py file to get output from it.
##code starts
def index(req):
return “Test successful”;
##code ends
Source link: http://ubuntuforums.org/showthread.php?t=91101 (Comment by Oscar on March 4th, 2006)
Thank you for the article, just a note for those who might click too fast during installers.
I was trying to install mod_python-3.3.1.win32-py2.5-Apache2.2.exe with my python-2.6.2 installation. This will not work and the warning during mod_python installation says you need python 2.5!
At the time of this writing, there is no mod_python for python 2.6. Make sure you install python-2.5.4.msi
I hope the tutorial could be updated to include Paul’s additionals Step in having Pythin enabled for all directories rather than the test directory only.
my apache server won’t restart after i type the loadmodule line, i’ve tried everything, python 2.5 is correctly installed and the mod_python.so is in the correct directory, it just wont restart untill i take this line out, any more suggestions anyone?
I have the same problem … ^
No offense, but I don’t think Apache is well-suited for Python. I’ve found IIS is a simpler and more compatible server for Python than Apache is. No need to install mod_python. Just register Python to IIS and it’s ready to generate web pages. Check this link: http://support.microsoft.com/kb/276494
I have only this question that after installing python. We’ll be able to run php applications also on xampp or we need to switch them???
Not sure, I think PHP will still work on xampp though…
I posted simple tutorial http://bit.ly/gqJJp6 of how to install python for XAMPP on my blog http://blog.promosquare.com. handy for those who are dealing with python first time.
Cool, thanks!
You asked, “Why did you choose Python over PHP or Ruby?”
Quite simply, Python is the scripting language ESRI uses for ArcGIS development.
Nice post, but note that mod_python is now no longer maintained and replaced by mod_wsgi.
See http://stackoverflow.com/questions/3287347/setting-up-python-on-apache-windows-ide-question and links in the top answer, or just go to mod_wsgi directly.
Hey I’ve heard about a new wamp stack called Ampps which is Python enabled using mod_python. Python version is 2.5.4 and mod_python 3.3.1. You can check out their website http://www.ampps.com