PyDev does not recognize PyQt5

I am following the pyqt tutorial and got this code:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        cb = QCheckBox('Show title', self)
        cb.move(20, 20)
        cb.toggle()
        cb.stateChanged.connect(self.changeTitle)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Checkbox')
        self.show()

    def changeTitle(self, state):
        if state == Qt.Checked:
            self.setWindowTitle('Checkbox')
        else: self.setWindowTitle('Unchecked!')

def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

      

I am using PyDev on Eclipse. Suffice it to say that the code works fine, but the awkward thing is that PyDev is underlining something with Qt / Q with a red line, which indicates it on hover Undefined variable: <..>

. If it's undefined, then how is it that my code works without errors? Understandably this must be a problem with PyDev. I removed the python interpreter (it was pointing to python2.7 instead of 3.4) and read it as the correct version; but it didn't work. Interestingly, it recognizes PyQt4 and insists on using widgets and not PyQt5.

Just so you guys know the example code above is from another notebook with PyQt5. Both projects were from PyDev and both were Ubuntu 15.04. It is possible that my project import on my current machine messed up PyDev parsing the required libraries. Does anyone have a solution why PyDev doesn't recognize PyQt5?

+3


source to share


1 answer


I had the same problem. These steps worked for me.

  • Set environment variable: export QT_API = pyqt5 (or something like that)
  • restart eclipse to select the new environment setting and then add PyQt5 to the list of forced built-in functions for the interpreter (Window-> preferences-> pydev-> interpers-> python interpers) or look here http://www.pydev.org/manual_101_interpreter .html for more details.


The next SO question prompted me to have a variable present: Setting up IPython Qtconsole with PyQt5 . Before I install it, I can get some shutdown just by adding "PyQt5" to the builtins, but it won't provide a complete list of additions to something like from PyQt5.QtGui import

, although ipython stand-alone will be. Also, the python console in pydev had the same problem and the call module_completion("from PyQt5.QtGui import Q")

fromIpython.core.completerlib

made the same incomplete list. In the end I figured out that since pydev was loading PyQt4 for the gui event loop (also configurable in the interpreter settings), there was a namespace conflict when it tried to explore Qt5 modules, forcing it to dig before it could create a complete list of addons ... Setting the environment variable causes pydev to load pyqt5 instead of the standard pyqt4. I haven't tested, but it seems likely that this way pydev will have pyqt4 link completion problems.

+3


source







All Articles