Python 3.6.5程序无法运行:弹出CMD类黑窗口后立即关闭求助
Hey there! I’ve run into this exact annoying issue dozens of times—your program finishes running so fast the CMD window closes before you can see what’s going on (or if there’s an error). Let’s walk through the easiest, most reliable fixes:
Quick Fix: Add a Pause to Your Script
The simplest way to keep the window open is to force your program to wait for input before exiting. Add one of these lines at the very end of your .py file:
input("Press Enter to close this window...")(my go-to—waits for you to hit Enter, and lets you read any output/errors first)import time; time.sleep(10)(pauses for 10 seconds automatically; adjust the number to make it longer/shorter)
Better for Debugging: Run the Script Manually from CMD
If you need to see error messages or full output, launching the script directly from Command Prompt is the best approach. Here’s how:
- Open Command Prompt (press
Win+R, typecmd, hit Enter) - Use the
cdcommand to navigate to your script’s folder. For example:cd C:\Users\YourUsername\Documents\PythonProjects - Run your script with Python:
python your_script_name.py
The window will stay open until you close it manually, so you can read every line of output or error.
If You Use a Shortcut: Tweak Its Properties
If you’re double-clicking a desktop shortcut to run your script, modify the shortcut to keep the window open:
- Right-click the shortcut → select Properties
- In the Target field, add
/kat the end of the command (before the closing quote). For example:- Original:
C:\Python36\python.exe C:\MyScripts\test.py - Modified:
C:\Python36\python.exe C:\MyScripts\test.py /k
- Original:
- Click Apply then OK. Now the window stays open after your script runs.
Use an IDE (No CMD Window Needed!)
Python 3.6.5 comes with IDLE, its built-in IDE. Just right-click your .py file → select Edit with IDLE, then click Run → Run Module. The output will show up in IDLE’s shell window, which never closes automatically. You can also use IDEs like PyCharm or Visual Studio Code for the same effect.
内容的提问来源于stack exchange,提问作者AskMath

