No name 'QApplication' in module 'PyQt5.QtWidgets' error in Pylint
Solution 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.
Solution 2
I think the simplest way to remove package import errors is by going into vscode's JSON settings by Ctrl+Shift+P, search "settings" and choose Preferences: Open Settings (JSON)
and adding this line to the dict:
"python.linting.pylintArgs": ["--extension-pkg-whitelist=PyQt5"]
If you want to add multiple packages, just add it with the first, separated by a comma like this:
"python.linting.pylintArgs": ["--extension-pkg-whitelist=PyQt5,otherPkg"]
Solution 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!
Solution 4
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.
Solution 5
If you use VSCode, go to "File" > "References" > "Settings" > click on this icon in top-left corner: (The "settings.json" file will be opened) > add these lines to "settings.json":
{
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=PyQt5"
]
}
wolfeyes90
Updated on July 12, 2022Comments
-
wolfeyes90 almost 2 years
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.
-
wolfeyes90 almost 5 yearsMac OS to add to this.
-
S. Nick almost 5 yearsTypo, change
w = QtWidget()
tow = QWidget()
and addsys.exit(my_app.exec_())
-
wolfeyes90 almost 5 yearsThanks, the typo fixed one of the issues, the other two are still there unfortunately
No name 'QApplication' in module 'PyQt5.QtWidgets'
andNo name 'QWidget' in module 'PyQt5.QtWidgets'
-
eyllanesc almost 5 years@wolfeyes90 1) If it is a typo that does not cause the error that you ask here then it corrects the code that you show to avoid confusion. 2) What version of Python3 do you use? What version of PyQt5? How have you installed PyQt5?
-
wolfeyes90 almost 5 years@eyllanesc 1. Updated the code to match the errors 2. Python is
3.7.3
, and my installed PyQt5 is5.12.2
. Yes it is installed, digging around on forums it seems to be related to lint not recognizing it since Qt is built in C, but, none of the solutions online to whitelist it have worked successfully. -
eyllanesc almost 5 yearschange
w = QtWidget()
tow = QWidget()
-
wolfeyes90 almost 5 years@eyllanesc I've changed this but when I added the error messages properly I did not update the code, same issues and new code is up there.
-
eyllanesc almost 5 years@wolfeyes90 I did not say it's the solution, I just pointed out that you correct that part to avoid confusion. On the other hand as you point out PyQt5 is a binding of Qt that is written in C++ and that generates these problems, but for those cases there are stubs, and in the latest versions it already provides them, maybe your IDE is not configured to use them, I am not Expert in VS Code so I can not point out the exact way but if a starting point.
-
-
wolfeyes90 almost 5 yearsThanks John, I appreciate you reproducing this on your machine. I've been scouring the internet and I found a solution that works, posted it here as well.
-
rustyBucketBay over 4 yearsnice finding! Helpfull for me
-
JonBrave over 4 yearsHelpful, but you cannot just "create a .pylintrc and put that line in" as the whole file (
configparser.MissingSectionHeaderError: File contains no section headers.
). You need to runpylint --generate-rcfile > .pylintrc
and then edit that, look forextension-pkg-whitelist=
in the[MASTER]
section -
JonBrave over 4 yearsHelpful, but you cannot just "create a .pylintrc and put that line in" as the whole file (
configparser.MissingSectionHeaderError: File contains no section headers.
). You need to runpylint --generate-rcfile > .pylintrc
and then edit that, look forextension-pkg-whitelist=
in the[MASTER]
section -
wolfeyes90 about 4 yearsMaybe your setup was a bit different, mine just took a
touch .pylintrc
to create the file and adding the extension whitelist code above. Either way, thanks for sharing, I'm sure this will help someone else out that my above solution doesn't work for. -
drekbour over 3 yearsYes but this is not an Answer to the question
-
8Observer8 over 3 yearsI found a conflict between PyQt5 and PySide2. I saw this error even after adding a line above. I just deleted this line and the problem was solved:
"--extension-pkg-whitelist=PySide2",
-
Thomas Sablik over 3 years@wolfeyes90 No, your setup is not different but your solution could be bad. You should create a .pylintrc file with default values and not an empty file.
-
AngeLOL over 3 yearsI had the same problem, ALE vim plugin didn't recognize some modules and I thought it was because the plugin, but then generated the rcfile and everything works now
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.