29

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
  • 1,022
  • 1
  • 8
  • 15
  • Mac OS to add to this. – wolfeyes90 Jun 23 '19 at 18:11
  • 2
    Typo, change `w = QtWidget()` to `w = QWidget()` and add `sys.exit(my_app.exec_())` – S. Nick Jun 23 '19 at 18:22
  • Thanks, the typo fixed one of the issues, the other two are still there unfortunately `No name 'QApplication' in module 'PyQt5.QtWidgets'` and `No name 'QWidget' in module 'PyQt5.QtWidgets'` – wolfeyes90 Jun 23 '19 at 18:37
  • @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? – eyllanesc Jun 23 '19 at 19:28
  • @eyllanesc 1. Updated the code to match the errors 2. Python is `3.7.3`, and my installed PyQt5 is `5.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. – wolfeyes90 Jun 23 '19 at 21:00
  • change `w = QtWidget()` to `w = QWidget()` – eyllanesc Jun 23 '19 at 21:01
  • @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. – wolfeyes90 Jun 23 '19 at 21:14
  • 1
    @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. – eyllanesc Jun 23 '19 at 21:22
  • Does this answer your question? [pylint can't find QWidget and QApplication](https://stackoverflow.com/questions/46337716/pylint-cant-find-qwidget-and-qapplication) – bers Oct 11 '22 at 07:02

8 Answers8

59

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.

wolfeyes90
  • 1,022
  • 1
  • 8
  • 15
  • nice finding! Helpfull for me – rustyBucketBay Oct 05 '19 at 23:02
  • 14
    Helpful, 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 run `pylint --generate-rcfile > .pylintrc` and then edit that, look for `extension-pkg-whitelist=` in the `[MASTER]` section – JonBrave Nov 19 '19 at 12:41
  • 1
    Maybe 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. – wolfeyes90 Feb 20 '20 at 03:03
  • @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. – Thomas Sablik Sep 28 '20 at 14:11
  • 1
    I 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 – AngeLOL Nov 23 '20 at 17:43
  • thanks, it helps. I just added .pylintrc manually and it works – Herahadi An Jan 05 '23 at 09:39
21

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"]
VagrantC
  • 687
  • 2
  • 13
  • 31
  • I 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",` – 8Observer8 Aug 24 '20 at 19:09
6

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!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
5

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.

john-hen
  • 4,410
  • 2
  • 23
  • 40
  • Thanks 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. – wolfeyes90 Jun 23 '19 at 23:19
3

If you use VSCode, go to "File" > "References" > "Settings" > click on this icon in top-left corner: enter image description here (The "settings.json" file will be opened) > add these lines to "settings.json":

{
    "python.linting.pylintArgs": [
        "--extension-pkg-whitelist=PyQt5"
    ]
}
8Observer8
  • 868
  • 10
  • 17
0

As suggested from @wolfeyes90 here Create a file on the root directory of the project named .pylintrc with the content:

extension-pkg-whitelist=PyQt5
Tadeu Sampaio
  • 135
  • 1
  • 8
  • 2
    Helpful, 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 run `pylint --generate-rcfile > .pylintrc` and then edit that, look for `extension-pkg-whitelist=` in the `[MASTER]` section – JonBrave Nov 19 '19 at 12:41
0

Try this

{
"python.linting.pylintArgs": ["--extension-pkg-whitelist=PyQt5"]
}
  • Your 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](/help/how-to-answer). – Community Jan 05 '22 at 07:35
-4

Maybe this error about PyQt5 installing