Double-clicking a Python file creates a temporary console window. When the program ends—successfully or with an exception—Windows closes that window immediately. The disappearing window is therefore a symptom, not the actual error.
Avoid adding random delays before you know what failed. First run the script from a terminal where the output remains visible.
Fix 1 — Run the script from Command Prompt
- Open the folder containing the script in File Explorer.
- Click the address bar, type cmd, and press Enter.
- Run the script:
python your_script.py
If the command itself fails, try:
py your_script.py
The terminal will remain open and display the exception, missing file, syntax error, or import problem.
Fix 2 — Read the full traceback
Start at the final line of the traceback because it states the exception type and message. Then move upward to the last line that points to your own file. That is usually where the failure began.
Common examples include:
ModuleNotFoundError— a package is missing from the active interpreter.FileNotFoundError— the script is looking in the wrong working directory.SyntaxError— Python could not parse the file.PermissionError— the program cannot read or write the target location.
Fix 3 — Check whether the program simply finishes quickly
A valid script such as print("Done") can finish in milliseconds. For tools intended to be launched by double-click, you may add a final input prompt:
input("Press Enter to close...")
Use this only after confirming there is no exception. It keeps the window open but does not repair underlying errors.
Fix 4 — Correct the working directory
Double-clicked scripts may start with a working directory different from the script’s own folder. A relative file path can then fail even when the file is visibly beside the script.
from pathlib import Path
base_dir = Path(__file__).resolve().parent
config_file = base_dir / "config.json"
Building paths from __file__ makes the script independent of where it was launched.
Fix 5 — Verify the correct Python version and environment
python -c "import sys; print(sys.executable)"
python --version
If the script depends on packages from a virtual environment, activate that environment before running it. Double-clicking normally does not activate project environments.
Fix 6 — Check the .py file association
If the file opens in an editor, the wrong terminal, or an old Python version, right-click it and inspect Open with. File associations are convenient for simple scripts but unreliable for projects with environment-specific dependencies.
Fix 7 — Run the script from VS Code correctly
- Open the project folder, not only the individual file.
- Select the correct Python interpreter.
- Use Run Python File in Terminal.
- Read the terminal output after the run.
Fix 8 — Log errors for scripts launched outside a terminal
For a utility that users will double-click, write unexpected errors to a log file:
import logging
logging.basicConfig(
filename="app-error.log",
level=logging.ERROR,
format="%(asctime)s %(levelname)s %(message)s"
)
try:
main()
except Exception:
logging.exception("Unhandled error")
raise
This provides evidence when the window closes before anyone can read it.
except: pass. That hides the failure and makes troubleshooting much harder.
FAQ
Does the closing window mean Python is broken?
Usually no. It means the process ended. Running the file from a terminal reveals whether it ended normally or crashed.
Why does the script work in VS Code but not by double-clicking?
VS Code may activate the correct environment and project directory, while double-clicking uses the default interpreter and a different working directory.
Should every script end with an input prompt?
No. Add one only for intentionally interactive, double-clicked console tools. It is unnecessary for normal terminal, IDE, automation, or server scripts.