Python script does not end properly

I have a python script that I call with python main.py on my terminal. It starts the Qt-GUI, which runs correctly and exits when I close the GUI.

However, sometimes the last debug message "over and out" is displayed, but the script itself does not end. Neither ctrl + c , ctrl + d , nor ctrl + z affect execution. It seems to me that this happens when an exception was thrown inside the program (and got into the GUI).

I don’t know how to debug this, since this is clearly not happening in the GUI itself. How do I debug this and find out what I did wrong?

 if __name__ == '__main__': import sys app = QApplication(sys.argv) form = MainGui() form.show() app.exec_() print "over and out" 

EDIT: It seems to me that there is some kind of thread at the end. However, I do not understand how to work with the stream (I do not know what Qt does internally ...). Is there a way to view all running threads at the end?

EDIT2: Oh my god. The solution just restarted my system. Somehow, my OS did some crazy things and prevented the script from shutting down.

+4
source share
2 answers

The solution just restarted my system. Somehow, my OS did some crazy things and prevented the script from shutting down.

0
source

"None of ctrl + c , ctrl + d and ctrl + z affects execution.

Add these lines of code to your program header, and ctrl + c will exit it.

 import signal signal.signal(signal.SIGINT, signal.SIG_DFL) 

And if you want to automatically go to the pdb debugger when your program gets an exception, just do this:

 import sys def excepthook(type_, value, tb): import pdb import traceback # print the exception... traceback.print_exception(type_, value, tb) print # ...then start the debugger in post-mortem mode pdb.pm() # we are NOT in interactive mode if not hasattr(sys, 'ps1') or sys.stderr.target.isatty(): # this stops PyQt from freezing the terminal from PyQt4.QtCore import pyqtRemoveInputHook pyqtRemoveInputHook() sys.excepthook = excepthook 
+1
source

Source: https://habr.com/ru/post/1384951/


All Articles