How to install Python with Wampserver

Python support can be added to WampServer fairly easily, similar to adding any Apache module that doesn’t ship with the base package. You need to take a few extra steps to make sure you can continue to use WampServer console to manage your application stack.

Download mod_wsgi Apache Module

You’ll need to get an appropriate binary version of mod_wsgi. WSGI is the preferred method for writing web applications in Python. The binary you use has to match up with the versions of Windows, Apache, and Python you are using. This site provides binaries for current versions: mod_wsgi binaries. NOTE: If you are at all concerned about security, you should probably compile your own version of mod_wsgi from the source code, or only download from a trusted source.

The binary goes into the Apache modules directory. In my case, I downloaded mod_wsgi-3.4.ap22.win-amd64-py2.7.zip (Windows 7 64-bit, Python 2.7, Apache 2.2). On my laptop, the correct directory was c:\wamp\bin\apache\Apache2.4.4\modules.

Update httpd.conf and refresh WampServer

Next, you need to update httpd.conf. This can be done from the WampServer control panel by Selecting Apache->httpd.conf from the menu. Notepad (or your default editor) will launch.

Find the section where there is a bunch of LoadModule statements. At the bottom of this section, add a LoadModule statement for mod_wsgi:

LoadModule wsgi_module modules/mod_wsgi.so

Save the httpd.conf file and exit Notepad.

To let WampServer know that you’ve made a change, you’ll need to refresh it. Right-click the WampServer icon in the system tray, and select Refresh. Now, when you look at the list of modules in the control panel (Apache->Apache Modules) you should see mod_wsgi in the list. If it isn’t already checked, go ahead and check it. If Apache doesn’t restart automatically, do so now from the control panel.

If Apache doesn’t start up, and you are sure you didn’t mistype the LoadModule statement above, then most likely you have a version mis-match between what your WampServer has installed, and the mod_wsgi binary you downloaded.

Hook up Apache to your Python Application

This step will vary depending on what application framework you are using (cherrypy, Django, etc). I’ll provide a really basic example to make sure everything is working correctly; this example closely follows the official WSGI documentation.

Create a directory to hold your WSGI application. I created a directory called C:\code\wsgi. In there, create a Python module that implements a function called ‘application’. This will be the entry point for your application whenever your application URL is called.

I called my module wsgi.py:

def application(environ, start_response):
    status="200 OK"
    output="Hello World!"

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output]

Next, you’ll need to update your Apache httpd.conf file to point WSGI at your application. In my use case, I was maintaining a PHP site while doing some prototyping of a new site using Python. To keep the two separate, I defined a virtual server in Apache, listening on a different port number. I also added the IfModule directive, so that if I disable mod_wsgi using the WampServer control panel, then these statements are ignored.

<IfModule wsgi_module>
    <VirtualHost *:8090>
        WSGIScriptAlias /myapp /code/wsgi/wsgi.py
        <Directory /code/wsgi>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
</IfModule>

Test It

Restart Apache using the WampServer control panel. If all is well, you should be able to type

http://localhost:8090/myapp 

into your browser, and see the Hello, World! message displayed.

Updated May 2015

Newer releases of the Windows binaries for mod_wsgi are packaged using the whl file format. The whl file is a Python PIP “wheel” file. It’s compatible with ZIP, so you can rename the file using a .zip extension to extract the mod_wsgi.so file (from the data directory).

Alternately, you can run ‘pip install (packagename).whl’ to install mod_wsgi.so as a Python package. You would have to find out where Python extracted the mod_wsgi.so file and copy it to the right place (if necessary).

I used the former approach for the latest version of WAMP Server. The correct file was mod_wsgi-4.4.11+ap24vc10-cp34-none-win32.whl. The ‘cpNN’ part of the name should match up with the version of Python you have installed.

Leave a Comment