How to keep a Python script output Window open [7 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
5 min

banner

# Table of Contents

  1. How to keep a Python script output Window open
  2. Interactively running a Python script without closing the output window
  3. Windows: Using drag and drop to run a Python script without closing it
  4. Keeping the Python script output window open by asking for user input
  5. Using a try/except statement to keep the Python output window open
  6. Using the atexit.register() method to keep the output window open

# How to keep a Python script output Window open

You can keep the Python script output Window open by running the script from your terminal.

  1. Open your terminal (e.g. Command Prompt (CMD) on Windows, or bash on macOS and Linux).
  2. cd to the directory where your Python script is located (e.g. main.py).
  3. Run the Python script from your terminal.
shell
python main.py # Or python3 python3 main.py # Or py alias (Windows) py main.py

The code sample assumes that your Python script is named main.py. Make sure to update the name if necessary.

For example, suppose that you have the following main.py file.

main.py
site = 'bobbyhadz.com' print(site) def greet(name): return f'Hello {name}' print(greet('bobby hadz'))

Now open your terminal in the same directory as the main.py file and run the following command.

shell
python main.py # Or python3 python3 main.py # Or py alias (Windows) py main.py

keep python script output window open

The output of the Python script will be shown in your terminal window and the Window will be kept open.

# Interactively running a Python script without closing the output window

You can also interactively run a Python script with the -i flag.

shell
python -i main.py # Or python3 python3 -i main.py # Or py alias (Windows) py -i main.py

interactively run python script without closing window

When you issue the command with the -i flag, the Python interpreter opens after your file is run.

You can directly run commands in the Python interpreter as shown in the screenshot.

Once you're done, call the exit() function.

shell
exit()

You can also start the interpreter without running a specific file by issuing the python command.

shell
python # Or python3 python3 # Or py alias (Windows) py

start interpreter without running file

Once you're done issuing Python commands, call the exit() function.

shell
exit()

# Windows: Using drag and drop to run a Python script without closing it

If you are on Windows, you can also:

  1. Press Win ⊞ + R and type cmd /k.

type cmd k

  1. Click OK.

  2. A new command prompt (CMD) window opens.

  3. Drag and drop your Python script into the CMD window.

  4. Press the Enter key.

Here is a short clip that demonstrates how this works.

drag and drop script into cmd window

Once you drag and drop the Python script into your CMD window, the path to the script will get pasted.

Press the Enter key to run the file.

# Keeping the Python script output window open by asking for user input

Alternatively, you can keep the Python script output window open by asking the user for input.

Suppose you have the following main.py file.

main.py
name = input('What is your name: ') print(f'Your name is {name}')

Open a terminal in the same directory as your main.py file and run it by issuing the following command.

shell
python main.py # or python3 python3 main.py # or py alias (Windows) py main.py

You will get prompted to enter a name.

keep python script output window open by asking for input

The input function can be used to ask the user for input.

The prompt waits for the user to enter a value and press Enter.

Note that the input() function converts the supplied value to a string and returns it.

The function is guaranteed to return a string even if the user enters an integer.

If you need to get an integer value, use the int class to convert the string to an integer.

main.py
num1 = int(input('Enter a number: ')) num2 = int(input('Enter another number: ')) result = num1 * num2 print(f'Multiplying {num1} by {num2} equals {result}')

keep script window open by asking for integer input

I've also written other detailed articles on how to take user input in Python:

# Using a try/except statement to keep the Python output window open

You can also use a try/except statement to keep the Python output window open.

Suppose you have the following main.py file.

main.py
def main(): # Your code here print('bobbyhadz.com') if __name__ == '__main__': try: # your code here main() except BaseException: import sys print(sys.exc_info()[0]) import traceback print(traceback.format_exc()) finally: print("Press Enter to continue...") input()

The code that you want to run should be placed in the main() function.

keep script window open using try except

We used an if statement to check if the user is running the Python script directly with python main.py.

The __name__ variable will only be equal to the string '__main__' if the user ran the script directly with python main.py.

If the main.py script is imported into another script, then the __name__ variable won't be equal to '__main__' and the if block won't run.

We call the main() function in the try block.

If an error occurs in the try block, it gets caught in the except BaseException block and gets printed to the terminal.

The finally block runs regardless if there was an error or not and allows you to view the output of the Python script.

You can press Enter to exit.

Here is a more minimalistic version of the previous code sample.

main.py
site = 'bobbyhadz.com' print(site) def greet(name): return f'Hello {name}' print(greet('bobby hadz')) print('Press Enter to continue...') input()

keep python script output window open until enter is pressed

You can press Enter once you're done.

# Using the atexit.register() method to keep the output window open

You can also use the atexit.register method to keep the output window open.

main.py
import atexit site = 'bobbyhadz.com' print(site) def greet(name): return f'Hello {name}' print(greet('bobby hadz')) atexit.register(input, 'Press Enter to continue...')

keep script window open using atexit register

The atexit.register method registers the given function to be run at termination.

The second argument we passed to the method gets passed to the input function.

You can press Enter to exit.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.