问题
Running into this issue in VS Code while trying to learn PyQt5, "No name 'QApplication' in module 'PyQt5.QtWidgets'", "No name 'QWidget' in module 'PyQt5.QtWidgets'"".
I'm not sure if this is a pylint issue or something else. I've confirmed PyQt5 is installed with pip3 list but I can't seem to figure out the issue.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
def app():
my_app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Test")
w.show()
sys.exit(my_app.exec_())
app()
I'd expect this error to not keep displaying but its preventing me from running things in VS Code. Any help or suggestions appreciated.
回答1:
I've figured out the issue, apparently Pylint doesn't load any C extensions by default, because those can run arbitrary code. So I found that if you create a system file in your project directory with the file named .pylintrc the rc file can whitelist this package to stop throwing errors by adding the following code in the rc file extension-pkg-whitelist=PyQt5. So essentially the issue isn't PyQt5, it was the linter throwing false errors due to this.
回答2:
I can reproduce the PyLint errors in Visual Studio Code on Windows 10 (Python 3.7.3, PyQt 5.11.3, PyLint 2.3.1). Though it doesn't prevent me from running the code, as the question suggests.
It is certainly a problem with the linter, not the PyQt5 installation or anything else, as PyLint stops complaining when changing the code to the following equivalent:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
window.setWindowTitle("Test")
window.show()
app.exec_()
The notable difference being that this code imports the QtWidgets module as a whole, not individual class objects defined in it.
回答3:
I found a solution easy, just use QApplication this way:
my_app = QtWidgets.QApplication(sys.argv)
and do not import QApplication from PyQt5.
Tested in PyQt5!
回答4:
As suggested from @wolfeyes90 here
Create a file on the root directory of the project named .pylintrc with the content:
extension-pkg-whitelist=PyQt5
来源:https://stackoverflow.com/questions/56726580/no-name-qapplication-in-module-pyqt5-qtwidgets-error-in-pylint