First, check whether Python finished or crashed.
Do not add delays, reinstall Python, or edit PATH just because the window disappears. Open a terminal that stays open, run the same file there, and let the output tell us which branch is real.
python at allThis is a command-discovery problem. Switch to the dedicated Python command journey instead of mixing two issues together.Decision rule: keep the terminal open until you can classify the run as normal completion, Python command failure, or an exception.
When you double-click a Python file in File Explorer, Windows can create a console for that process. When the Python process ends, the console can disappear immediately. That does not prove Python is broken. The useful evidence is what the script prints before it exits.
Why the Python Window Closes Immediately
There are two common cases. A short script can complete in a fraction of a second, so the console vanishes before you can read its output. Or the script can crash with an exception and the temporary console disappears immediately after Python exits. Both look almost identical when you only double-click the file.
That is why the first move is always the same: run the file from a Command Prompt or PowerShell window that you opened yourself.
Run the Script From Command Prompt Instead
- Open the folder that contains your
.pyfile in File Explorer. - Click the address bar, type cmd, and press Enter. Command Prompt opens directly in that folder.
- Run the script:
python your_script.py
If the plain python command is unavailable but the Python launcher works on your machine, try:
py your_script.py
The terminal now stays open after the process exits, so you can read the complete result instead of chasing a flashing window.
How to See the Error Before the Window Disappears
If you now see a Python traceback, start with the final line. It normally gives the exception type and message. Then look upward for the last line that points to your own script.
ModuleNotFoundErrorusually means the active interpreter cannot find the imported package.FileNotFoundErrorusually means a file path or working-directory assumption is wrong.SyntaxErrormeans Python could not parse the code at the reported location.PermissionErrormeans the process was blocked from reading, writing, or opening the target resource.
If the error says a module is missing even though you installed it, use the ModuleNotFoundError after pip install journey rather than installing the package repeatedly.
If the Script Finished Normally
If the terminal shows the expected output and then returns to the prompt with no traceback, the script did not crash. It simply finished. For example, this program ends immediately after printing:
print("Done")
That is normal behavior. If the program is meant to be run from a terminal, no fix is required.
If the program is intentionally designed for people to double-click and read a console result, you can choose to pause at the end. Do that only after proving there is no hidden exception.
If the Script Crashed With an Error
Fix the exception shown in the persistent terminal. Do not treat the close itself as the root problem. If you are not sure which interpreter is running the script, check it directly:
python -c "import sys; print(sys.executable)"
If you used py to run the file, use the same launcher for the check:
py -c "import sys; print(sys.executable)"
This is especially useful when the script works in one IDE but fails when launched another way, because the two launch methods may be using different Python environments.
If the error says a file cannot be found
A script launched by double-click can behave differently when it relies on a relative path. For a standalone script that needs files stored beside it, build the path from the script location instead of assuming the current working directory:
from pathlib import Path
base_dir = Path(__file__).resolve().parent
config_file = base_dir / "config.json"
Use this only when the resource is genuinely relative to the script. User-supplied paths should still be handled as user-supplied paths.
If Nothing Appears at All
First make sure the file is actually a .py script and not a .pyw file. Python's windowed launcher (pythonw) and .pyw scripts are designed to run without a normal console window.
Then run the file from Command Prompt using the steps above. If there is still no output and no traceback, the script may simply not print anything, may exit before reaching the code you expect, or may be waiting on a GUI/background action instead of console output.
You can add a temporary checkpoint while debugging:
print("Reached this point")
Move that checkpoint through the program to find the last section that executes. Remove temporary debug prints when the problem is understood.
If Python Itself Is Not Recognized
If Command Prompt responds with a message such as 'python' is not recognized, python was not found, or opens the Microsoft Store instead of running your file, stop this page here. The script-close problem cannot be diagnosed reliably until Windows can reach the intended Python runtime.
Should You Add input() to Keep the Window Open?
Only when the script is intentionally meant to be double-clicked as an interactive console tool and you already proved it finishes normally. A simple pause is:
input("Press Enter to close...")
This changes when the program exits; it does not repair an exception. Adding it before reading the traceback can hide the real troubleshooting path.
If you want a separate launcher without changing the Python program, a small batch file can run the script and then pause:
@echo off
python your_script.py
pause
Use the launcher command that already works on your machine. If python is not the correct launcher, fix that first rather than hard-coding the wrong runtime.
Verify the Script Runs Correctly
Verification gate — prove the original problem is understood
Before calling it fixed, rerun the same script from the persistent terminal and confirm the result matches one of these outcomes:
- The script completes normally and returns to the prompt with no traceback.
- The script waits for the intended user input instead of disappearing unexpectedly.
- If you fixed an exception, the same traceback no longer appears.
- If the script uses nearby files, it still works when launched from the method you actually plan to use.
Related Python Startup Problems
Reviewed references
The troubleshooting boundaries and Python behavior on this page were checked against current Python documentation.
