I'm creating an executable from my Python script now, and when something fails in the script (for example, a file is not present), I quit the script with sys.exit('*Enter reason here*')
. This works excellent in the terminal view, because the output is still visible in the window. However, when I build an executable, the window is closed immediately, and the reason why the scripts ends, is not readable.
Is there an option to keep the command window open (preferably in PyInstaller)?
I found an option:
Because sys.exit()
raises the error SystemExit
it can be catch in a try-except statement. Even the text within sys.exit()
can be catched! Because there is only one function that is called (main_function), it is a short and comprehensible option:
if __name__ == '__main__':
try:
main_function()
except SystemExit as e:
print 'Error!', e
print 'Press enter to exit (and fix the problem)'
raw_input()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With