5 Easy Ways to Keep a Python Script Open After Termination

5/5 - (1 vote)

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 your_program.py.

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()

Use the 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()

Use the 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 Win+R, type 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!