qtconsole
qtconsole copied to clipboard
Missing error messages when using PyQt5 event loop integration
I'm trying to have exceptions from a Qt application print to the Jupyter QtConsole and also have the application not block input to the QtConsole (event loop integration). This was working with PyQt4 and IPython 4.2, but with PyQt5 and IPython 5.1 I can either have exceptions printed and block input, or have no exceptions printed with event loop integration.
In Jupyter QtConsole, running the following and clicking the button doesn't print the exception. If %gui qt5 is skipped, it does print the exception but the application blocks the console.
%gui qt5
run test.py
test.py:
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
import sys
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.pushButton = QPushButton('test', self)
self.pushButton.clicked.connect(self.testfunc)
def testfunc(self):
print('hello')
print(9 / 0)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
app.exec_()
Side notes: even without %gui qt5, the exception is only printed if the following fix is applied: https://github.com/jupyter/qtconsole/pull/141. Also, I can't get the exception to print at all in Spyder's IPython console, after trying various settings (inline graphics vs. automatic vs. Qt5).
OS: Windows 7 Distribution: Anaconda 4.2 python: 3.5.2 ipython: 5.1.0 qtconsole: 4.2.1 pyqt: 5.6.0 jupyter: 1.0.0 spyder: 3.0.1
I can reproduce this, but when the exceptions don't show up in the Qt console, they are showing up in the terminal from which it was launched. The Qt event loop must be handling the errors, otherwise they would crash the application; I guess that under some circumstances it is writing them to C-level stderr rather than using sys.excepthook.
Thanks, I didn't realize the exceptions were in the terminal since I was launching Jupyter QtConsole from the start menu shortcut which doesn't open a terminal window.
I was a little confused by this: What should sys.stdout/sys.stderr be when you launch a script with run in qtconsole? The terminal or the qtconsole stream?
sys.stdout and sys.stderr are the capturing streams that send data to the Qt console. But code can still write to the low-level stdout and stderr file descriptors (1 and 2), which go to the terminal. In particular, if C/C++ code prints output, it will bypass the Python-level capturing and go to the terminal.
Well, I just tried running with test.py as:
print("printing")
raise ValueError("here")
And while it prints, the exception doesn't show up anywhere (neither terminal nor console).
Is that before or after the merge of #141? Are you doing anything with PyQt event loops when that goes wrong?
Ah, missed that. After pulling master, running the code above produces no exception info either in terminal or in the console (Win7).
Was this actually fixed? I think there was still a problem related to the Qt event loop after #141 landed.
(I don't think it's strictly a Qt console problem - it's more likely to be something in the kernel - but I want to work this out properly before closing)