I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can an
Start the script from already open cmd window or at the end of script add something like this, in Python 2:
raw_input("Press enter to exit ;)")
Or, in Python 3:
input("Press enter to exit ;)")
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
print "hello world"
python hello.py
If you want to stay cmd-window open AND be in running-file directory this works at Windows 10:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
In python 2 you can do it with: raw_input()
>>print("Hello World!")
>>raw_input('Waiting a key...')
In python 3 you can do it with: input()
>>print("Hello world!")
>>input('Waiting a key...')
Also, you can do it with the time.sleep(time)
>>import time
>>print("The program will close in 5 seconds")
>>time.sleep(5)
Using atexit, you can pause the program right when it exits. If an error/exception is the reason for the exit, it will pause after printing the stacktrace.
import atexit
# Python 2 should use `raw_input` instead of `input`
atexit.register(input, 'Press Enter to continue...')
In my program, I put the call to atexit.register
in the except
clause, so that it will only pause if something went wrong.
if __name__ == "__main__":
try:
something_that_may_fail()
except:
# Register the pause.
import atexit
atexit.register(input, 'Press Enter to continue...')
raise # Reraise the exception.