pyqt4 – simple IPython example raises exception on sys.exit()

pyqt4 – simple IPython example raises exception on sys.exit()

This answer is thanks to Matthias BUSSONNIER from the ipython-users mailing list.

When I close the window of the running demo application, I see this error:
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0

Just dont use sys.exit(0) as you are not exiting python, but still running IPython.

Add it back if you wish to run your app from a (real) command line and have a return status.

If I try to execute my code again, I get this:
RuntimeError: A QApplication instance already exists.

This is a PySide Bug that they wont fix as they dont consider it a bug.


QApplication can only have one instance and quitting an app is
apparently not considered a reason sufficient enough do delete the

You can use this code from above issues :

app=QtGui.QApplication.instance() # checks if QApplication already exists 
if not app: # create QApplication if it doesnt exist 
     app = QtGui.QApplication(sys.argv)

This was a sufficient solution for my present needs.

What you need to do is to cause the QApplication to be deleted later as in:

app = QApplication(sys.argv)

Using this code you can rerun the application as many times as you want in IPython, or anywhere else and every time you close your qt application, the object will be deleted in python.

pyqt4 – simple IPython example raises exception on sys.exit()

sys.exit just raises SystemExit to terminate the interperter.

ipython catches SysExit when it executes a script in interactive mode, so this isnt acutally an error but a feature of ipython do avoid the interactive interpreter from being shutdown when a script is executed, as thats not what you usually want in an interactive session.

Leave a Reply

Your email address will not be published.