I've got a problem debugging python PyQt5-application in vscode. Unlike described in some older forum discussions (i.e.: https://github.com/microsoft/ptvsd/issues/428) breakpoints are working fine in every part of code, but the debugger may not break/stop on raised exceptions in Qt-slot functions executed following a Qt-signal. I was able to make the small example below. When reaching the raised exception the debugger terminates execution. When I put a breakpoint in that line, the debugger breaks/stopps as expected. I wasn't able to solve this issue. Does anybody know how to solve this issue? Is there any settings to be put in vscode? I didn't find the flag "--qt-support" mentioned in the above linked discussion - and don't know whether it would help for this problem.
I've tested a few more things. I tried different conditions/rules for the breakpoints. When activating "Raised Exceptions" (only then, I've tried all combinations), the debugger stopps where the error is raised. I don't know whether the core code of PyQT catches the raised error, but in my code it's not raised. Therefore, when the only activated condition is "User Uncaught Exceptions" it should stop in the breakpoint.
Just for completeness, I've also tested the suggestion (from here: How to debug PyQt5 threads in Visual Studio Code?) to add debugpy.debug_this_thread(). It doesn't change anything for my problem.
It seems to be an issue with the vscode debugger (or wanted behaviour which I don´t understand. My workaround is:
- Activate Breakpoints only for "Raised Exceptions"
- In launch.json: "justMyCode": true (else the debugger breaks in many externally raised and catched exceptions)
To my understanding, "User Uncaught Exceptions" should do what I want. Could anybody tell whether the described behaviour is as it should be? Is there a way to get the vscode debugger to break only in exceptions which are not caught in my code?
Thanks a lot for your help!
Kind regards, Jerome
"""
python 3.8.13
pyqt 5.15.7
ms-python.python v2022.16.1
visual studio code 1.72.2
"""
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (
QPushButton,
QApplication,
QMainWindow,
)
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.b = QPushButton("Button", self)
self.b.setCheckable(True)
self.b.toggle()
self.b.clicked.connect(lambda: self.do_on_click(self.b))
self.setWindowTitle("Button demo")
def do_on_click(self, b):
# If breakpoint is set in next row, the debugger stopps,
# else debugger is terminated - not abled to break on exception.
raise(Exception("This is to provocate the debugger mal-function."))
print("Clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
