How to enable Python on IIS.
Article Number: 000031673 | Last Modified: 2019/01/17
Sometimes developers may want to make their mashups hosted on a site being added into IIS but can interpret Python code as well. Like in some scenarios a developer may run an Ajax call to query a Python file, but then for this communication to work fine the *.py file needs to be interpreted by IIS, hence this article will explain how to enable this for IIS, the IIS version used in this test is 1607 running on Windows 10.
- Download and install Python form https://www.python.org/
- Install the 64x version if you have a 64-bit machine.
- Unpack and install the Python installation file, you may choose the C:\ directory so you can easily find the Python exe file later when working with IIS setup. The default installation path anyway will be something like C:\Python27.
- Make sure the IIS feature is turned on for your Windows OS, you can do this by opening the Windows Features menu. In the list scroll until you find Internet Information Services. Expand this option and check Web Management Tools and World Wide Web Services > IIS Management Console. Then press Ok, this will enable IIS on your system.
- Make sure CGI is also installed, you can find this under IIS > Application Development Features > CGI. Mark this option.
- On your desktop create a folder PythonProject for example and place a Python file in it, in my case I created MyPythonApp.py. All it does is to print a hello world message in the browser.
- In the windows search bar type IIS, then in the search result you will get the Internet Information Services (IIS) Manager as the best match. Run it, if you face issues later in the prepress with permissions then re-run IIS with administrator rights. If you never done this before then simply right click on the application and click on Run as administrator from the pop up menu.
- Expand the Sites as you see in the image below and select Add Website..
- Fill in the necessary information needed by your site, see image below as a guide:
- Right click on your newly added site and set the permissions on the Python project directory and its content. To do that select the Edit Permissions… option from the pop up menu. In the project properties menu go to the Security tab, press on the Edit button and then add the following user names (note in my case as an internal person working at Qlik I had to add these Group and user names): IUSR and give it Full control rights in the Permissions For IUSR section.
Then I had to also add SELUN-YHA\IIS_IUSRS and give it Full control rights in the Permissions For IIS_IUSRS section.
- Now we need to map between the site in IIS and the CGI / Python interpreter. The steps for that are:
- Select / highlight the site from the list of Sites.
- In the UI options to the right, there is an icon under IIS, the icon says Handler Mappings. Double click on it so that a new menu will appear to the left, with a title saying Actions.
- Click on Add Script Map… after this a pop up will appear with some fields you need to fill in.
- The Request path is for specifying the type of files the interpreter will be able to process, in this case we should put *.py for Python files.
- Executable, this is the path to the Python.exe file (installed in the previous steps in the C:\). In my case, it put C:\Python27\python.exe %s %s
- In the Name field, you can type a name like Python Interpreter. The image below illustrates my setup:
- Enable Directory Browsing for your project, this way when the site is launched you can see a listing of the files that are part of your web application and you will be able to click on which one you wish to execute.
IIS site enable directory browsing.png
- One last step before we can begin to see any result, right click on your site, select Manage Website > Advanced Settings > Application Pool > Set this to DefaultAppPool.
- Now you may check if your site is in the running state. Right click on your site, select Manage Website > Browse, then your default browser will open with a list of files you have in your web project, in this case it is PythonProject as indicated above.
- Since we want to test Python so from the list simply click on the file with *.py extension, in this case it is MyPythonApp.py. If everything was configured properly then you will get a result like this:
- Finally, if you click on MyPythonApp.py, you should get the following result in the end:
- Source code for the Python application is attached to the article.