PyDev doesn't recognise PyQt5

旧巷老猫 提交于 2021-01-27 16:08:33

问题


I am following a tutorial on pyqt, 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'm using PyDev on Eclipse. Suffice it to say that the code runs fine, but what is awkward is that PyDev underlines anything Qt/Q with a red line which when hovered over says Undefined variable: <..>. If it is undefined then how is it that my code runs without errors? Clearly this ought to be a problem with PyDev. I've removed the python interpreter (it was pointing to python2.7 instead of 3.4) and readded it as the correct version; but that didn't work. Interestingly enough, it recognises PyQt4 and insists on using widgets from that instead of PyQt5.

Just so you guys are aware, the code sample above is from another laptop which had PyQt5 as well. Both projects were from PyDev, and both had Ubuntu 15.04. It's possible that my importing of the project on my current machine messed up PyDev parsing the required libraries. Does anyone have a solution as to why PyDev doesn't recognise PyQt5?


回答1:


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

  1. Set the environment variable: export QT_API=pyqt5 (or whatever as appropriate)
  2. restart eclipse so that picks up the new environment setting, and then add PyQt5 to the list of forced builtins for the interpreter (Window->preferences->pydev->interpreters->python interpreters) or look here http://www.pydev.org/manual_101_interpreter.html for more details.

The following SO question tipped me off to the presence of the variable: Setting up IPython Qtconsole with PyQt5. Before I set it, I as able to get some completion to work just by adding 'PyQt5' to the builtins, but it would not, for example, provide the full list of completions to something likefrom PyQt5.QtGui import, even though ipython stand-alone would. Further, the python console in pydev had the same problem and calling module_completion("from PyQt5.QtGui import Q") from Ipython.core.completerlib produced the same incomplete list. In the end, I guessed 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 introspect the Qt5 modules, causing it to bail out before it could build the full list of completions. Setting the environment variable causes pydev to load pyqt5 instead of the default pyqt4. I haven't checked, but it seems likely that set this way pydev will have problems completing pyqt4 references.




回答2:


For all those lonesome internet wanderers trying to figure out how to integrate eclipse, pydev, and pyqt5 on Linux, I bring you my method from start to finish.

Eclipse, PyQt5, and PyDev on Linux

  1. Install python v3.6
  2. Install eclipse from eclipse.org
  3. In eclipse, click Help->Install New Software
  4. Click Add...
  5. Add in software source "http://www.PyDev.org/updates" to the available software sources
  6. Call it PyDev
  7. Click on PyDev checkbox
  8. Install it by clicking Next
  9. Download PyQt5
  10. Download SIP
  11. Install SIP first
  12. Install PyQt5
  13. Reconfigure eclipse to use PyQt5
  14. Click on Window→Preferences→PyDev→Interpreters→Python Interpreters
  15. Click on Advanced Auto-Config
  16. Rename interpreter to “python3.6”
  17. Click on Libraries tab
  18. Click on New Folder
  19. Add in “/usr/lib/x86_64-linux-gnu/qt5/plugins”
  20. Add in “/usr/lib/x86_64-linux-gnu/qt5/libexec”
  21. Add in “/usr/lib/x86_64-linux-gnu/qt5/bin”
  22. Click Apply
  23. Click Apply and Close
  24. Restart eclipse
  25. Profit!

This will allow you to get the tab code completion in eclipse when developing pyqt5 applications.



来源:https://stackoverflow.com/questions/31101925/pydev-doesnt-recognise-pyqt5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!