PyCharm PyQt4 / PyQt5 collision in debug mode

2019-08-27 09:19发布

问题:

I am trying to use PyQt4 in pycharm. My code works perfectly in run mode, but in debug mode, when I try to import PyQt4 I get the following error:

"RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class"

This happens even w/ very barebones code:

from PyQt4 import QtGui, QtCore
print('cheese')

(thus, this is different from previous PyQt4 Pycharm problems w. Matplotlib)

Clearly, the PyCharm debugger is using PyQt5 (this can be seen by calling sys.modules['PyQt5']). How can I 'un-import' PyQt5, or at least prevent the collision?

Also: I tried importing differently to include explicit dependencies, but this also gives error:

import PyQt4 as pp
pp.QtGui

AttributeError: module 'PyQt4' has no attribute 'QtGui'

Thanks!

回答1:

you can go to Settings>Build,Execution,Deployment>Debugger>Python Debugger>PyQt compatible: Select PyQt4.



回答2:

Try going to File > Settings > Project > Project Interpreter. Edit your current interpreter, or create a new one, and remove PyQt5 from the list that shows up.