Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in function to keep command window open in PyInstaller

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)?

like image 652
Mathias711 Avatar asked Oct 18 '22 18:10

Mathias711


1 Answers

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()
like image 170
Mathias711 Avatar answered Oct 27 '22 18:10

Mathias711