导入 PyQt5 时出现 RuntimeError

Posted

技术标签:

【中文标题】导入 PyQt5 时出现 RuntimeError【英文标题】:RuntimeError when importing PyQt5 【发布时间】:2016-09-26 23:20:50 【问题描述】:

我现在正在学习 PyQt5

当我尝试运行以下代码时

from PyQt5.QtWidgets import QApplication

我犯了错误

Traceback (most recent call last):
File "<ipython-input-10-0ebe4054b7ae>", line 1, in <module>
from PyQt5.QtWidgets import QApplication
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

那怎么了?谢谢帮忙!

【问题讨论】:

parts of both PyQt4 and PyQt5 are being imported at some point。您应该能够在两行中重现相同的错误:from PyQt4.QtCore import QObjectfrom PyQt5.QtCore import QObject 我用另一台笔记本电脑运行我以前的代码,但没有遇到错误 【参考方案1】:

问题是 Matplotlib 使用 QT5 而您使用的是 QT4。为了解决这个问题,你可以强制 Matplotlib 使用 QT4,通过编辑 matplotlibrc,它位于 \Lib\site-packages\matplotlib\mpl-data

在我的例子中是 matplotlib

【讨论】:

【参考方案2】:

This 解决了我的问题:

你可以去 Settings>Build,Execution,Deployment>Debugger>Python Debugger>PyQt compatible: 选择 PyQt4。

【讨论】:

以上是关于导入 PyQt5 时出现 RuntimeError的主要内容,如果未能解决你的问题,请参考以下文章