Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple IPython example raises exception on sys.exit()

I'm doing some very simple PySide (and PyQt) tutorials in IPython. One tutorial just creates a window with some sliders to demonstrate slots and signals.

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
To exit: use 'exit', 'quit', or Ctrl-D.

So I run %tb and get this:

SystemExit                                Traceback (most recent call last)
/Workspaces/scratch/<ipython-input-1-88966dcfb499> in <module>()
     33 
     34 if __name__ == "__main__":
---> 35     main()

/Workspaces/scratch/<ipython-input-1-88966dcfb499> in main()
     29         w.show()
     30         app.exec_()
---> 31         sys.exit(0)
     32 
     33 

SystemExit: 0

If I try to execute my code again, I get this:

RuntimeError: A QApplication instance already exists.

In case it helps, here my code:

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class MyWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self, None)

        vbox = QVBoxLayout(self)

        self.slider1 = QSlider(Qt.Horizontal)
        self.slider1.setRange(0, 99)
        self.slider1.setValue(0)
        vbox.addWidget(self.slider1)

        self.slider2 = QSlider(Qt.Horizontal)
        self.slider2.setRange(0, 99)
        self.slider2.setValue(99)
        vbox.addWidget(self.slider2)

        self.slider1.valueChanged.connect(self.slider2Changed)

    def slider2Changed(self, position):
        self.slider2.setValue(self.slider2.maximum() - position)

def main():
        app = QApplication(sys.argv)
        w = MyWindow()
        w.show()
        app.exec_()
        sys.exit(0)

if __name__ == "__main__":
    main()

I do not have errors when running the code using python:

python myexample.py

This error only happens when I run the code in an IPython (including a notebook or the qtconsole or the regular ipython terminal).

UPDATE: My main problem is that I cannot run the application again quickly and easily. If I try to run my code again, I get this:

RuntimeError: A QApplication instance already exists.

That kills the fast, interactive nature of IPython :(

like image 948
MountainX Avatar asked Jun 04 '12 20:06

MountainX


People also ask

Is raised by the sys exit () function?

In Python, SystemExit is an exception that is raised by the sys. exit() method. In general, all exceptions must be derived from BaseException which are instances of a class.

Does SYS exit throw exception?

Raise a SystemExit exception, signaling an intention to exit the interpreter. When called, the sys. exit() function will raise a SystemExit exception.

How to exit function with error in Python?

You can pass an integer to sys. exit() if you'd like to exit with a non-zero return code (usually signaling some sort of failure condition). You can also pass a string to sys. exit() , which will get printed to the command line, along with a return code of 1 .


1 Answers

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 don't 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 "won't fix" as they don't consider it a bug.

See https://github.com/ipython/ipython/issues/1124)
and http://bugs.pyside.org/show_bug.cgi?id=855

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

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.

like image 123
MountainX Avatar answered Oct 20 '22 06:10

MountainX