Last updated: Apr 11, 2024
Reading time·5 min
try/except
statement to keep the Python output window openatexit.register()
method to keep the output window openYou can keep the Python script output Window open by running the script from your terminal.
cd
to the directory where your Python script is located (e.g. main.py
).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.
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.
python main.py # Or python3 python3 main.py # Or py alias (Windows) py main.py
The output of the Python script will be shown in your terminal window and the Window will be kept open.
You can also interactively run a Python script with the -i
flag.
python -i main.py # Or python3 python3 -i main.py # Or py alias (Windows) py -i main.py
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.
exit()
You can also start the interpreter without running a specific file by issuing
the python
command.
python # Or python3 python3 # Or py alias (Windows) py
Once you're done issuing Python commands, call the exit()
function.
exit()
If you are on Windows, you can also:
Win ⊞
+ R
and type cmd /k
.Click OK.
A new command prompt (CMD) window opens.
Drag and drop your Python script into the CMD window.
Press the Enter
key.
Here is a short clip that demonstrates how this works.
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.
Alternatively, you can keep the Python script output window open by asking the user for input.
Suppose you have the following main.py
file.
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.
python main.py # or python3 python3 main.py # or py alias (Windows) py main.py
You will get prompted to enter a name.
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.
num1 = int(input('Enter a number: ')) num2 = int(input('Enter another number: ')) result = num1 * num2 print(f'Multiplying {num1} by {num2} equals {result}')
I've also written other detailed articles on how to take user input in Python:
try/except
statement to keep the Python output window openYou can also use a try/except
statement to keep the Python output window open.
Suppose you have the following main.py
file.
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.
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
.
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.
site = 'bobbyhadz.com' print(site) def greet(name): return f'Hello {name}' print(greet('bobby hadz')) print('Press Enter to continue...') input()
You can press Enter
once you're done.
atexit.register()
method to keep the output window openYou can also use the atexit.register method to keep the output window open.
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...')
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.
You can learn more about the related topics by checking out the following tutorials: