You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows Server IIS部署Python应用?部署后显示代码问题求助

Troubleshooting Python App Showing Raw Code on IIS 2012

Hey there, let's work through why your Python app is displaying raw code instead of executing on Windows Server 2012's IIS. That #!iusr line you noted in the tutorial is part of the solution, but there are several other critical checks we need to cover:

  • Add the correct shebang line to your script
    While Windows doesn't use shebangs like Unix systems, IIS relies on this line to map your script to the Python interpreter. At the very top of your app.py, add:

    #!C:\full\path\to\your\python.exe
    

    Replace the path with your actual Python installation location (e.g., #!C:\Python38\python.exe). If you're using a virtual environment, point to the python.exe inside your venv's Scripts folder. This path must match the one you configured in your handler mapping.

  • Verify your handler mapping configuration
    Double-check the handler mapping you set up for .py files:

    • Ensure the executable path includes %s %s at the end (this passes the script path and arguments to Python). For example: C:\Python38\python.exe "%s" "%s"
    • Confirm the request path is set to *.py (to target all Python scripts)
    • Under Request Restrictions, make sure the Access dropdown is set to Script (not Read or None)
  • Enable the CGI feature in IIS
    IIS won't execute Python scripts unless the CGI role service is enabled. To turn it on:

    1. Open Server Manager → Add Roles and Features
    2. Navigate to Web Server (IIS) → Web Server → Application Development
    3. Check the CGI box and complete the installation
  • Adjust application pool settings
    Your app pool needs to be configured for non-.NET applications:

    • Set the .NET CLR Version to No Managed Code
    • Confirm the app pool identity (e.g., ApplicationPoolIdentity or IUSR) has Read & Execute permissions on your Python installation directory and website files
  • Test with a minimal script first
    Rule out issues with your business logic by testing a simple "Hello World" script:

    #!C:\Python38\python.exe
    print("Content-Type: text/html\n")
    print("<h1>Hello from Python on IIS!</h1>")
    

    If this script runs correctly, you can gradually integrate your business code while ensuring each part follows the same execution rules.

  • Check IIS logs for errors
    If you're still stuck, look into IIS's log files (typically stored at C:\inetpub\logs\LogFiles) for specific error codes or messages. 500-level errors often hint at permission issues or misconfigured paths.

内容的提问来源于stack exchange,提问作者Mohit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:18:17