How to keep a Python script output window open?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The simplest way to keep a Python script's output window open is to add input("Press Enter to exit...") as the last line of your script. This pauses execution and waits for a keypress before the console closes. The problem primarily occurs on Windows when double-clicking a .py file, because Windows opens a new cmd.exe window that closes as soon as the Python process terminates.
Why the Window Closes
When you double-click a .py file on Windows, the system launches python.exe script.py in a new console window. Once the script finishes (or crashes), the Python process exits and the console window closes immediately. You never see the output or the error traceback.
This does not happen when you run scripts from an already-open terminal (Command Prompt, PowerShell, or a Unix shell) because the terminal remains open after the child process exits. It also does not happen in IDEs like PyCharm, VS Code, or IDLE because they manage their own output panels.
Method 1: input() at the End of the Script
The most common and portable solution:
This works on every platform. The script runs normally and pauses at the end, keeping the window open until you press Enter.
Handling Exceptions
If your script crashes with an unhandled exception, execution never reaches the input() call and the window still closes before you can read the traceback. Wrap your main logic in a try/except:
The finally block ensures the pause happens whether the script succeeds or fails. traceback.print_exc() prints the full exception traceback so you can diagnose the error.
Method 2: Run from the Command Line
Instead of double-clicking the file, open a terminal first and run the script from there:
The terminal stays open after the script finishes, and you can see all output including error messages. This is the recommended approach for development.
Method 3: Create a Wrapper Batch File (Windows)
If you or your users prefer double-clicking to run scripts, create a .bat file that runs the script and pauses:
Save this as run_script.bat in the same directory as your Python script. The %~dp0 prefix resolves to the batch file's directory, so it works regardless of the working directory. pause displays "Press any key to continue..." and keeps the window open.
For a version that also shows errors:
Method 4: Use python -i (Interactive Mode)
The -i flag tells Python to drop into an interactive interpreter after the script finishes:
After the script runs, you get a >>> prompt where you can inspect variables, call functions, or debug:
To make this the default when double-clicking .py files on Windows, you can change the file association, but this affects all Python scripts and is usually not desirable.
Method 5: Configure Your IDE
Most IDEs keep the output panel open after execution. If you are developing in an IDE, you generally do not need any of the above workarounds.
| IDE | Output behavior | Configuration |
| PyCharm | Run panel stays open | Default behavior, no changes needed |
| VS Code | Terminal stays open | Use integrated terminal (Ctrl+`) |
| IDLE | Shell window stays open | Default behavior |
| Thonny | Shell stays open | Default behavior, designed for beginners |
| Jupyter Notebook | Output persists in cell | Default behavior |
For VS Code specifically, make sure you are running scripts in the integrated terminal, not with the "Run Without Debugging" command that uses the debug console:
Method 6: Windows Registry (Change Default Behavior)
If you want all .py files to keep the window open when double-clicked, you can modify the Windows registry to use cmd /k python instead of just python:
The /k flag tells cmd.exe to execute the command and then remain open. To revert:
This is a system-wide change and affects all users. Use it cautiously.
Method 7: os.system("pause") (Windows Only)
A Windows-specific alternative that calls the pause command:
This displays "Press any key to continue..." just like a batch file. It works only on Windows because pause is a cmd.exe built-in command. For cross-platform scripts, prefer input().
Comparison of Methods
| Method | Cross-platform | Catches crashes | Requires code changes | Best for |
input() at end | Yes | No (unless wrapped in try/finally) | Yes | Quick scripts, beginners |
input() in try/finally | Yes | Yes | Yes | Production scripts run by non-developers |
| Run from terminal | Yes | N/A (terminal stays open) | No | Development workflow |
| Wrapper .bat file | Windows only | Yes (with pause) | No | Distribution to Windows users |
python -i | Yes | Yes (drops to REPL) | No | Debugging |
| IDE | Yes | Yes | No | Daily development |
| Registry change | Windows only | No | No | System-wide default change |
Common Pitfalls
Putting input() inside a function that is not always called. If input() is inside a conditional branch or a function that only runs in certain cases, the script exits without pausing in the other cases. Always put the pause in the if __name__ == "__main__" block at the top level.
Using input() without a try/finally block. An unhandled exception kills the script before reaching input(). The window closes and you never see the traceback. Always wrap in try/finally if crashes are possible.
Using time.sleep() instead of input(). Sleeping for a fixed number of seconds is fragile. If the output is long, users cannot scroll and read before the timer expires. If the output is short, they wait unnecessarily. input() gives the user control over when to close.
Using os.system("pause") in cross-platform code. This only works on Windows. On macOS or Linux it prints sh: pause: command not found and exits immediately. Use input() for portable code.
Forgetting to remove the pause before deploying. Scripts that run as scheduled tasks, cron jobs, or services should not pause for input. They hang indefinitely waiting for a keypress that never comes. Use a guard:
sys.stdin.isatty() returns True only when the script is running in an interactive terminal, not when piped or run as a background service.
Summary
The window-closing problem is specific to double-clicking .py files on Windows. The most reliable fix is input("Press Enter to exit...") in a try/finally block at the end of your script, which handles both normal execution and crashes. For development, run scripts from an already-open terminal or use an IDE that keeps the output panel open. For distributing scripts to non-technical users on Windows, pair the .py file with a .bat wrapper that includes pause. Use sys.stdin.isatty() to conditionally skip the pause when the script runs non-interactively.
Related reading
- How to keep index when using pandas merge
- How to keep keys/values in same order as declared?
- How to know scikit-learn confusion matrix's label order and change it
- How to know which Python is running in Jupyter notebook?
- How to kill a process on a port on ubuntu
- How to kill a process running on particular port in Linux?
- How to know/change current directory in Python shell?
- How to leave/exit/deactivate a Python virtualenv
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.