简体   繁体   English

使用PyDev + Eclipse调试PyQt应用程序

[英]Debugging a PyQt application using PyDev+Eclipse

I routinely use PyDev in Eclipse for Python development. 我通常在Eclipse中使用PyDev进行Python开发。 However, I'm trying PyQt for the first time in the same environment. 但是,我是在同一环境中第一次尝试PyQt。 It works well, with one exception. 它运作良好,但有一个例外。 If the program errors out anywhere within the Qt main event loop, including within my own code, no error information is output to the PyDev console. 如果程序在Qt主事件循环内的任何地方(包括我自己的代码内)出错,则没有错误信息输出到PyDev控制台。

To demonstrate this I created the following simple PyQt app: 为了证明这一点,我创建了以下简单的PyQt应用程序:

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication

# test.ui contains a single Push Button named pushButton
base, form = uic.loadUiType("../ui/test.ui") 

class MainWindow(base, form):
    def __init__(self, parent=None):
        super(base, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.button_pressed)

    def button_pressed(self):
        print('button pressed')
        print(invalid_variable) # intentional error
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("fusion")
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

If I place an intentional error such as print(invalid_variable) anywhere after if __name__ == '__main__': and before the app.exec_() command, the program properly terminates with a traceback and the expected NameError: name 'invalid_variable' is not defined . 如果在if __name__ == '__main__':之后的任何地方if __name__ == '__main__': app.exec_()命令之前的任何地方放置了诸如print(invalid_variable)之类的故意错误,则程序将正确地终止于回溯,并且预期的NameError: name 'invalid_variable' is not defined However, if I press the button in my dialog using the above code, button pressed appears on the console but then the application terminates silently and with no error information in the console. 但是,如果我按在使用上面的代码我对话框中的按钮, button pressed控制台上显示,但随后在应用程序终止默默在控制台没有错误信息。 Other debugging operations, such as breakpoints and expressions, work fine. 其他调试操作(例如断点和表达式)也可以正常工作。

Is this expected behavior? 这是预期的行为吗? If so, what do you recommend for facilitating debugging of PyQt apps in this environment. 如果是这样,您建议如何在此环境中调试PyQt应用程序。 If not, I'd appreciate insight into what I need to do to rectify the problem. 如果不是这样,我将不胜感激,了解我需要采取哪些措施来纠正此问题。

  • Python version: python-3.4.3.amd64 python版本:python-3.4.3.amd64
  • PyDev version: PyDev for Eclipse 4.3.0.201508182223 PyDev版本:适用于Eclipse 4.3.0.201508182223的PyDev
  • PyQt version: PyQt5-5.5-gpl-Py3.4-Qt5.5.0-x64 PyQt版本:PyQt5-5.5-gpl-Py3.4-Qt5.5.0-x64

Update 2015-09-30 : 更新2015-09-30

When I run the test app directly with python.exe, it outputs the error correctly to stderr in both error scenarios. 当我直接使用python.exe运行测试应用程序时,在两种错误情况下,它都会将错误正确输出到stderr So this issue does seem specific to the PyDev environment. 因此,此问题似乎确实是PyDev环境所特有的。 I additionally created an entirely fresh Eclipse workspace with only the Python interpreter configured and ran my test again on a freshly created PyDev project using the same source code. 我还创建了一个完全新鲜的Eclipse工作区,仅配置了Python解释器,并使用相同的源代码在新创建的PyDev项目上再次运行了测试。 The results were the same. 结果是相同的。

This is a very old Question, but i thought I'd provide the answer since it shows up on google searches for this problem. 这是一个非常古老的问题,但我认为我会提供答案,因为它出现在Google搜索此问题上。

The issue is changes to pyqt5.5+ handling of errors, including the following code will reenable stacktrace on crashes. 问题是对pyqt5.5 +处理错误的更改,包括以下代码将在崩溃时重新启用stacktrace。

It took Alot of random browsing on the internet, but eventually found the code needed to work around the problem, posted here for reference for anyone else who has the same issue. 它在互联网上进行了大量随机浏览,但最终找到了解决该问题所需的代码,在此发布以供遇到相同问题的其他人参考。 Was obvious when I finally found it. 当我终于找到它时很明显。 ;) ;)

from PyQt5 import QtCore
import traceback, sys


if QtCore.QT_VERSION >= 0x50501:
    def excepthook(type_, value, traceback_):
        traceback.print_exception(type_, value, traceback_)
        QtCore.qFatal('')
sys.excepthook = excepthook

Hope this helps others. 希望这对其他人有帮助。

I had the same problem. 我有同样的问题。 And i added just one line: 我只添加了一行:

import ipdb

So all my QT applications are showing me errors. 因此,我所有的QT应用程序都向我显示错误。 ipdb - this is module for debugging your script, but without any setting (import only) it's works as you need ipdb-这是用于调试脚本的模块,但无需任何设置(仅导入),即可根据需要工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM