Chomper Stomping
jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected



Software

September 13, 2008

Installing Python (mod_python) on XAMPP (on Windows)

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

  1. Enter the text (leave out the space between phpinfo and ()):
    <? phpinfo (); ?>
    into a new text document in notepad.
  2. Save the document as “test.php” (or something) in the htdocs directory of your xampp install.
  3. 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).
  4. 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…
  5. 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

  1. Double click the exe file
  2. Click “Next”
  3. It should find your Python install. If you have more than one version, select the one you want to use. Click “Next”
  4. Click “Next”
  5. 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

  1. Open c:\xampp\apache\conf\httpd.conf
  2. 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.

  3. 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>
  4. Save and close the file.
  5. Restart Apache
  6. Create a new file (and folder) c:\xampp\htdocs\test\mptest.py
  7. 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).




About the Author

Christopher McCulloh
E-Commerce developer at Finish Line Co-Author of HTML, XHTML and CSS All-in-one Desk Reference for Dummies Graduated from IU with a Bachelors of Media Arts and Science and a Certificate in Applied Computer Science. Tech Editor for Building Facebook Applications for Dummies and Building Websites All-in-one for Dummies 2nd Edition. Creator and maintainer of the Status-bar Calculator Firefox Extension Three years professional experience in Java E-Commerce Development and four years professional experience with PHP for a combined total of seven years professional JavaScript/HTML/CSS experience




 
 

 
blue-xl

WordPress Settings API – Adding Options to Existing Page

Adding new options to an existing page in the dashboard in wordpress can be maddening. I’ve literally spent 15+ hours dealing with this horrible API at this point. To the point where I wrote two different wrappers for it....
by Christopher McCulloh
0

 
 
octocat

Introducing GitScripts

GitScripts is a project that attempts to make Git user friendly. I have been working on it for almost a year now. When we implemented Git in my office, we were having a really hard time using it. We loved the flexibility of the...
by Christopher McCulloh
1

 
 
sshlogo

Using two different identity files with ssh for rsa remote authentication keys

I have two different servers I need to connect to, each requiring two different types of remote authentication keys. One requires rsa, the other dss. So I had to make and use two different remote authentication keys, but was un...
by Christopher McCulloh
0

 

 
ATG
java-logo

Creating ATG Droplets and serving a default oparam

Creating your own ATG droplets is not difficult. Servicing a default open parameter (oparam) in an ATG droplet is surprisingly extremely easy. ATG has these things called “droplets” that you use from within your ...
by Christopher McCulloh
1

 
 
logo

Updates – BASIC jquery ui tabs rotate documentation, a note on nodejs hosting, and a note on the re-design

nodejs, jquery ui tab rotate, and re-design. Just a few quick notes… I’m actively working on documentation for the jquery ui tab rotation plugin. I’ve (finally) got a very basic working example up. The plugin ...
by Christopher McCulloh
0

 




20 Comments


  1. rossdavidh

    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!


  2. Dan

    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!


  3. Paul

    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


  4. Paul

    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!


  5. Paul

    Arrgh still mangled – should read replace % with ‘less than sign’ or ‘greater than sign’ as appropriate.


  6. Sid

    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.


  7. Sid

    replace % % with the appropriate angle brackets.

    i.e. %Directory /test% … %/Directory% #<– This is wrong!

    But, the following works:

    %Directory “C:/xampp/htdocs/test”% … %/Directory%


  8. Wahid Sadik

    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)


  9. Lisa

    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


  10. Someone

    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.


  11. 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?


  12. I have the same problem … ^


  13. tri

    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


  14. superman

    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???


  15. Not sure, I think PHP will still work on xampp though…


  16. 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.


  17. Leta Rogers

    You asked, “Why did you choose Python over PHP or Ruby?”

    Quite simply, Python is the scripting language ESRI uses for ArcGIS development.


  18. dirk

    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.


  19. James.G

    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



Leave a Reply

Your email address will not be published. Required fields are marked *

*


6 + four =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>