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
If you want to run your script from a desktop shortcut, right click your python file and select Send to|Desktop (create shortcut)
. Then right click the shortcut and select Properties. On the Shortcut tab select the Target: text box and add cmd /k
in front of the path and click OK. The shortcut should now run your script without closing and you don't need the input('Hit enter to close')
Note, if you have more than one version of python on your machine, add the name of the required python executable between cmd /k and the scipt path like this:
cmd /k python3 "C:\Users\<yourname>\Documents\your_scipt.py"
`import sys,traceback
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
simply write the above code after your actual code. for eg. am taking input from user and print on console hence my code will be look like this -->
`import sys,traceback
nam=input("enter your name:")
print("your name is:-{}".format(nam)) #here all my actual working is done
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
On windows 10 insert at beggining this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Strange, but it work for me!(Together with input() at the end, of course)
cmd /k
is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k
and then drag&drop the script you want to the Run dialog.
On Python 3
input('Press Enter to Exit...')
Will do the trick.
A simple hack to keep the window open:
counter = 0
While (True):
If (counter == 0):
# Code goes here
counter += 1
The counter is so the code won’t repeat itself.