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

Windows系统XAMPP运行Python脚本报End of script output before headers错误求助

Hey there, sorry to hear you're stuck with this frustrating CGI error—let’s break down some targeted fixes that often work when the usual solutions fall short on Windows XAMPP:

1. Make Sure Your Script Outputs Headers First (Non-Negotiable)

The "End of script output before headers" error almost always means your Python script didn’t send the required HTTP headers before content. Double-check that your script starts with this exact code (no print statements or imports before this):

print("Content-Type: text/html")
print()  # Critical: empty line to separate headers from content

For Python 3, ensure print() is used correctly (no missing parentheses) and that there’s no accidental output (like debug prints) before these headers—even a single stray character will break it.

2. Fix the Shebang Line for Windows

Windows doesn’t natively recognize Unix-style shebangs, but Apache still uses them to find the Python interpreter. Replace the generic shebang with the absolute path to your Python executable (use forward slashes or escaped backslashes):

#!/C:/xampp/python/python.exe
# OR if using system Python: #!/C:/Python311/python.exe

Also, save your script as UTF-8 without BOM—a BOM character at the start will mess up Apache’s ability to read the shebang.

3. Set Proper File Permissions & Paths

Windows permissions can be tricky for Apache:

  • Right-click your Python script → Properties → Security tab. Ensure the user running Apache (usually SYSTEM or your Windows username) has Read & Execute permissions.
  • Avoid spaces or non-ASCII characters in your script’s path (e.g., rename htdocs/My Scripts to htdocs/my_scripts). Apache often chokes on special characters in file paths.
4. Validate Apache’s Configuration Files

Double-check your Apache configs to make sure CGI is properly enabled:

  1. Open xampp/apache/conf/httpd.conf and confirm this line exists (add it if missing):
    AddHandler cgi-script .py
    
  2. Open xampp/apache/conf/extra/httpd-xampp.conf and find the directory block for your script’s location (e.g., htdocs). Add ExecCGI to the Options line:
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    

Don’t forget to restart Apache after making config changes!

5. Test with a Minimal Working Script

Rule out script-specific issues by creating a super simple test script (e.g., test.py in htdocs):

#!/C:/xampp/python/python.exe
print("Content-Type: text/html")
print()
print("<h1>CGI Test Success!</h1>")

Visit http://localhost/test.py in your browser. If this works, your original script has a bug (like unhandled exceptions, early exits, or misplaced output). If it fails, the problem is in your Apache/Python setup.

6. Dig Into Apache’s Error Logs

XAMPP keeps detailed error logs that will tell you exactly what’s going wrong. Open xampp/apache/logs/error.log and look for entries related to your script. You might see:

  • A Python traceback (indicating a syntax error or crash in your script)
  • "No such file or directory" (meaning Apache can’t find your Python interpreter)
  • Permission denied messages (confirming permission issues)
    These logs are your best debugging tool—don’t skip this step!
7. Add Python to Your System PATH

Adding your Python installation directory to Windows’ system PATH can help Apache locate the interpreter reliably. After updating PATH, restart your computer and then restart XAMPP to apply the changes.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:31:33