wolfeyes90
wolfeyes90

Reputation: 1122

No name 'QApplication' in module 'PyQt5.QtWidgets' error in Pylint

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.

Upvotes: 34

Views: 32138

Answers (8)

Rishikesh Karkhanis
Rishikesh Karkhanis

Reputation: 1

Try this

{
"python.linting.pylintArgs": ["--extension-pkg-whitelist=PyQt5"]
}

Upvotes: 0

8Observer8
8Observer8

Reputation: 1152

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"
    ]
}

Upvotes: 3

elifSOZ
elifSOZ

Reputation: 1

Maybe this error about PyQt5 installing

Upvotes: -4

VagrantC
VagrantC

Reputation: 827

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"]

Upvotes: 23

Tadeu Sampaio
Tadeu Sampaio

Reputation: 157

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

extension-pkg-whitelist=PyQt5

Upvotes: 0

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!

Upvotes: 8

john-hen
john-hen

Reputation: 4866

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.

Upvotes: 5

wolfeyes90
wolfeyes90

Reputation: 1122

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.

Upvotes: 64

Related Questions