Skip to content

Running Python on your OSX Apache server: mod_python

2010 August 8
by Tedb0t

Want to interpret Python scripts in Apache on your OSX installation?  Look no further!

After about 4 hours of repeated head-to-wall collisions, I finally managed to get mod_python running with Apache 2.2.14 on my Snow Leopard machine.  Here’s what I had to do:

  1. Use the MacPorts Apache instead of MAMP.  I absolutely could not get this to work for MAMP, which might be because MAMP uses version 2.062 of Apache.  All the following instructions are for MacPorts installs.  Once you install MacPorts, you can install Apache by doing
    sudo port install apache
  2. Now you need mod_python, which is a module for Apache that allows the HTTP server to use the python interpreter “inside” the server.  With MacPorts, just do
    sudo port install mod_python

    If for some reason this doesn’t work, I’ve packaged it up along with the mod_python python package described next.  Put it in

  3. Make sure you have the mod_python Python package (yeah, it’s got the exact same name as the Apache module but is a completely different thing).  Get into your Python interpreter and try
    import mod_python

    If it works, rejoice.  If not, I’ve included mine in a zip because I could not for the life of me figure out where to get it from, but I had it in one of my other Python installs. It goes here:

  4. Edit your httpd.conf file by adding the following:
    LoadModule python_module modules/
    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On

    This first tells Apache to load the module, which is a shared object file (.so).  Technical note for the curious: A shared object is a binary that contains compiled functions that can be directly addressed in memory, so you can call functions in it given a header file.  It’s similar to a Windows .dll.

    Anyway, the rest tells Apache to use mod_python’s VERY handy “publisher” function, described here.  This is a REALLY fast and easy way to start writing Python programs for the web.  Follow the examples there and you should be good to go!  Enjoy!

Related Posts:

2 Responses
  1. January 23, 2012

    … or you could just use CGI:
    In /etc/apache2/users/[username].conf add the ‘ExecCGI’ Option to the ‘Options’ directive and append ‘AddHandler cgi-script .py’ to the end. This would be inside the directory configuration stanza of course.

  2. mybofy permalink
    January 30, 2014

    Mavericks without OSX Server
    I have spent much time to search a solution to the same problem.
    The 2. works simply with MacPorts upgraded.
    I want to thank you for your tutorial.

Comments are closed.