Python · Windows Fix

Python Script Opens and Closes Immediately? See the Error Before Fixing It

If a .py file flashes on screen and disappears, the window closing is only the symptom. The script either finished normally or exited with an error. Run it from a terminal first so the real result stays visible.

Difficulty: EasyTime: 5–15 minutesReviewed: October 1, 2026Windows 10 / 11
ROHITH · DIAGNOSE BEFORE CHANGING CODE

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.

You see normal output and no tracebackThe script probably finished successfully. The quick close is expected when it was launched in a temporary console.
You see a traceback or errorThe close was caused by the process exiting after an exception. Fix the final error message, not the disappearing window.
You cannot run 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

  1. Open the folder that contains your .py file in File Explorer.
  2. Click the address bar, type cmd, and press Enter. Command Prompt opens directly in that folder.
  3. 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.

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.

Switch to the matching FIX

Diagnose the Python command on Windows →

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:

Stop and change paths when: the terminal shows a Python command problem, a package/environment mismatch, or a specific exception. Use the dedicated matching FIX rather than adding more pause code.

Related Python Startup Problems

Reviewed references

The troubleshooting boundaries and Python behavior on this page were checked against current Python documentation.