If you found this website, chances are you have the following question:
💬 Question: “I am new to Python. When I run a Python script file on Windows, the output window appears and then quickly disappears. I need to be able to view the output of my script, so how can I keep the output window open?”
Method 1: Add input() Before Ending Script
Use Python’s built-in
input() function to prompt the user for input before closing the window.
Here’s an example:
# Prompt the user for input name = input("Please enter your name: ") # Output the name back to the user print("Hello, " + name + "!") # Wait for user confirmation: input('Confirm by pressing any key')
Method 2: Run Program From Terminal
You can run your Python script from a terminal. For example, execute
python your_program.py to run the code stored in your file
You can also use the interactive mode in a terminal. The following Gif shows how I ran a simple code program in the Powershell terminal (Windows). Powershell is available in any Windows installation!
👉 Recommended: How to Run a Python Script?
Method 3: Use Infinite While Loop
Use an infinite loop that continuously prints an empty string to the output window. This will prevent your code from terminating and you can watch what it did.
# Prompt the user for input name = input("Please enter your name: ") # Output the name back to the user print("Hello, " + name + "!") # Wait until user presses CTRL+C while True: print('')
Do you want to know how to end the while loop to leave the program? Check out this tutorial on the Finxter blog:
👉 Recommended: How to Stop a While Loop?
Method 4: Use time.sleep()
time.sleep() function to pause the script execution for a brief period of time. For example,
time.sleep(10) at the end of your script will pause the script for 10 seconds.
import time # Your Script Here # Pause the script execution for 5 seconds time.sleep(5)
Method 5: Use os.system()
os.system() function to run a command in the shell to keep the window open until the user presses
Enter. This may be exactly what you need!
import os # Your Script Here # Keep the window open until user presses enter os.system("pause")
Bonus (Method 6) for Windows
To keep a Python script open after running it, press
cmd /k and then drag and drop the script into the Run dialog. This will open the script in a console window which will remain after the application closes.
Here’s how that looks like after opening the dialog:
It has already opened the IDLE editor by default which ships with any Python installation.
Now you can run the code in the editor. If you want to run it in the console itself, you can type
python in front of the full path, i.e.,
python C:\Users\xcent\Desktop\code.py in my case. The same trick with pulling in the file to get the path works for this case too.
👉 Recommended: Python IDLE vs PyCharm
Thanks for Coming By ♥️
If you want to keep learning with more practical coding projects, check out our email academy. It’s free and we have cheat sheets too!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.