Python on IIS: how?

Posted on

Question :

Python on IIS: how?

I’ve got a background in PHP, dotNet and am charmed by Python. I want to transpose functionality from PHP to Python step by step, running bits and pieces side-by-side. During this transition, which could take 2 years since the app is enormous, I am bound to IIS. I’ve got 15 years background of web-programming, including some C work in an ISAPI module on IIS which is the kind of work I don’t want to dive into any more.

It seems Python just doesn’t run well on IIS. I’ve struggled with FastCGI (not supported, just for PHP) and PyIsapie (badly documented, couldn’t get it up and running). In the end I got it up and running with a HeliconZoo dll BUT:

My next problem is: how to debug/develop a site? In PHP you install a debugger and whenever you have a problem in your website, you just debug it, set a breakpoint, step through code, inspect watches and such. It seems to me this is the most rudimentary type of work for a developer or troubleshooter. I’ve bought WingIDE which is an excellent tool and debugger but it can’t hook into the Python instance in the IIS process for some reason so no debugging. I noticed Helicon starts Python with -O so I even recompiled Python to ignore this flag altogether but my debugger (WingIDE) just won’t come up.

I can set up a PHP ‘hello world’ website on IIS in half an hour including download time. I think I’ve spent about 120 hours or more getting this to work for Python to no avail. I’ve bought Programming Python and Learning Python which is about 3000 pages. And I’ve googled until I dropped.

I think Python is a great language but I’m on the verge of aborting my attempts. Is there anyone who can give me a step-by-step instruction on how to set this up on IIS7?

Asked By: Robert


Answer #1:

I just did this in 5 minutes.

  1. Insure you have IIS. run: %windir%system32OptionalFeatures.exe. Or, via pointy-clicky: Start…Control Panel…Programs and Features… (and then on the left hand side) Turn Windows Features on or Off. Make sure CGI is installed, under the IIS node.

    enter image description here

  2. Download Python for Windows, from . I grabbed Python2.7. Make sure you get the x64 version if you have an x64 version of Windows.

  3. Unpack and install that python MSI. Choose the default, which puts python into c:Python27

  4. Create a directory to hold your “development” python scripts. Eg, c:devpython

  5. Set the permissions on the files in the directory c:devpython to allow IIS to read and execute. Do this by running these two icacls.exe commands from the command line:

    cd devpython
    icacls . /grant "NT AUTHORITYIUSR:(OI)(CI)(RX)"
    icacls . /grant "BuiltinIIS_IUSRS:(OI)(CI)(RX)"
  6. Open IIS manager. Run %windir%system32inetsrviis.msc, or do this via the control panel: Start…Control Panel…Administrative Tools…Internet Information Services (IIS) Manager. Create a new application. Specify the virtual path as /py and the physical path as c:devpython.

    enter image description here

    enter image description here

  7. Within that IIS application, add a script map for *.py, and map it to c:python27python.exe %s %s

    enter image description here

    enter image description here

    enter image description here

  8. create a “” file in c:devpython with this as the content:

    print('Content-Type: text/plain')
    print('Hello, world!')
  9. invoke http://localhost/py/

Answered By: Cheeso

Answer #2:

just make sure the path to the directory holding the cgi scripts doesn’t have spaces or &.

i tried lots of things for many days and nothing worked then i changed the path and it worked

If it has spaces, put quotes around the path, but not the %s %s
like this:

"C:Program FilesPython36python.exe" %s %s

Answered By: YEH

Answer #3:

When you are developing a web application with Python, you don’t use IIS/Apache/etc. Those web servers are only for deployment. Frameworks like Pyramid/Pylons/Django all come with built-in web servers. Pyramid, in particular, has excellent documentation which should help you to get started:

When you get to the point of deployment, Linux + Apache would be a much saner choice than Windows + IIS. If you absolutely must use Windows + IIS, don’t use isapi-wsgi, as it has phantom performance problem:

PyISAPIe has worked well enough for me, but I had to compile my own PyISAPIe.dll for Python 2.7.

Answered By: sayap

Leave a Reply

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