Python on IIS: how?

PythonIis

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

I just did this in 5 minutes.

  1. Insure you have IIS. run: %windir%\system32\OptionalFeatures.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 python.org . 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:\dev\python

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

     cd \dev\python
     icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
     icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"
    
  6. Open IIS manager. Run %windir%\system32\inetsrv\iis.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:\dev\python.

    enter image description here

    enter image description here

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

    enter image description here

    enter image description here

    enter image description here

  8. create a "HelloWorld.py" file in c:\dev\python with this as the content:

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

Solution 2 - Python

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

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

"C:\Program Files\Python36\python.exe" %s %s

Solution 3 - Python

Since this is quite an old question with some old answers (no accepted answer), here is a more up to date approach. The Microsoft doc below has a step-by-step guide to host Python apps through IIS (seems like it could work for Azure App Services as well - although MS suggests using Linux) making use of FastCGI (better approach post 2021 than the CGI approach).

The TL;DR is:

  • Install Python for Windows
  • Install wfastcgi package per https://pypi.org/project/wfastcgi/
  • Configure your web.config file to point to whichever framework your Python service is wrapped in (eg. Bottle, Flask, Django)

https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022

Solution 4 - Python

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: http://docs.pylonsproject.org/docs/pyramid.html

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: http://groups.google.com/group/isapi_wsgi-dev/browse_thread/thread/9fade6efca6c5b89

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertView Question on Stackoverflow
Solution 1 - PythonCheesoView Answer on Stackoverflow
Solution 2 - PythonYEHView Answer on Stackoverflow
Solution 3 - PythonJohan FoleyView Answer on Stackoverflow
Solution 4 - PythonsayapView Answer on Stackoverflow