Posts Tagged ‘python’

Running Django on Windows: Python 2.7.2 x64, PyISAPIe and IIS 7.5

March 13th, 2012 4 comments


Ok so I wanted to try serving Django from IIS 7.5 using Python 2.7.  The advice available says to install PyISAPIe.  Unfortunately there is not a compiled version of this available for Python 2.7 (x64 in my case).  geographika has a great post on compiling one yourself.  The following instructions are based on his guide, updated with newer versions.  If you don’t want/can’t compile it, here is my compiled version: PyISAPIe-trunk-r201-Py2.7.2×

Install Python

I did this on Win2k8 R2 Standard with VS2010.

I used ActiveState’s Python from (Windows 64bit

Install Python

Compile PyISAPIe

(or you can skip this and unzip PyISAPIe-trunk-r201-Py2.7.2× into c:\PyISAPIe (remove the PyISAPIe-trunk-r201-Py2.7.2×64 folder)

cd \temp

# If you want to duplicate what I’m doing exactly add -r 201 to get the revision I got

svn co pyisapi-trunk

robocopy c:\python27 pyisapi-trunk\Python\x64\Python27 /mir

open pisapi-trunk\PyISAPI.sln in VS2010

Change target to Release|x64

Right click PyISAPIe project, Properties, Configuration Properties->C/C++

Change Preprocessor PYI_PY_VERSION=PY_27 to PYI_PY_VERSION=PY_27_2

Open PyISAPIe.h and insert the following after #define PY_27

#define PY_27_2   (PY_27 + 2)


I got a couple of IntelliSense errors, but you can safely ignore those.

PyISAPIe.dll is produced under pyisapi-trunk\x64\Release

mkdir c:\PyISAPIe

copy pyisapi-trunk\x64\Release\PyISAPIe.dll c:\PyISAPIe

# Case may be important here

robocopy pyisapi-trunk\PyISAPIe\Python\Http c:\PyISAPIe\Http

Configure Website

Edit the properties -> security settings of c:\pyisapie\PyISAPIe.dll, add “Network Service” and tick “read” permission. (So IIS can use it).

# This is where we’ll put our python web app

mkdir c:\inetpub\PyApp

copy pyisapi-trunk\PyISAPIe\Python\Examples\ c:\inetpub\PyApp

Open IIS Manager

Create new site called PyApp, set physical path to c:\inetpub\PyApp, set port to 8090 (or whatever is unused)

Select PyApp, Handler Mappings->Open Feature

Click Add Script Map” with the following setting
Request Path = *
Executable = C:\PyISAPIe\PyISAPIe.dll
Name = PyISAPIe

Ok now browse http://localhost:8090/ and it should work!

Install Django

Download Django 1.3.1 from

Extract and run python install

cd c:\inetpub\PyApp

# Create test Django project startproject djangotest

Stick the following in djangotest\

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello world!")

Replace djangotest\ with:

from django.conf.urls.defaults import *
urlpatterns = patterns('',
    # Example:
    # (r'^newtest/', include('')),
    (r'^.*$', 'djangotest.helloworld.index'),
    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),

python djangotest\ runserver

now browse to to make sure the internal Django server serves the site ok

Before Django will work in IIS, you need just a couple more steps.

copy /y c:\PyISAPe\Examples\Django\*.py c:\PyISAPIe\Http

Now edit C:\PyISAPIe\Http\ after ‘Import DjangoMap’ add:

import os
# Probably a better way to do this, but this will get us going!

then jump down a bit and replace the following:
["/1" , "mysite1.settings" ], # first Django app
["/djangotest" , "djangotest.settings" ], # first Django app

Ok now try browsing http://localhost:8090/djangotest (or whatever port you configured the site on). You should see “Hello World!”. As geographika noted, you will need to recycle the PyApp app pool if you change any files on disk. I may try his modifications on’s Request method to get immediate feedback.

That seemed to have done it. I’ll try to post an update after I’ve been using it a bit.

Very Helpful Links

Compiling a 64bit Version of PyISAPIe –

Setting up Python on IIS7 –

Django Tutorial –

My Compiled PyISAPIe for Python 2.7.2 x64 – PyISAPIe-trunk-r201-Py2.7.2×

Categories: Uncategorized Tags: , ,