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:
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.
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.
Windows permissions can be tricky for Apache:
- Right-click your Python script → Properties → Security tab. Ensure the user running Apache (usually
SYSTEMor your Windows username) has Read & Execute permissions. - Avoid spaces or non-ASCII characters in your script’s path (e.g., rename
htdocs/My Scriptstohtdocs/my_scripts). Apache often chokes on special characters in file paths.
Double-check your Apache configs to make sure CGI is properly enabled:
- Open
xampp/apache/conf/httpd.confand confirm this line exists (add it if missing):AddHandler cgi-script .py - Open
xampp/apache/conf/extra/httpd-xampp.confand find the directory block for your script’s location (e.g.,htdocs). AddExecCGIto theOptionsline:<Directory "C:/xampp/htdocs"> Options Indexes FollowSymLinks ExecCGI AllowOverride All Require all granted </Directory>
Don’t forget to restart Apache after making config changes!
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.
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!
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

