Recently, I was working on a PyQt5 application. These days my editor of choice is Visual Studio Code. I use pylint for linting the code. I noticed pylint is complaining about ‘QApplication’ import as you can notice the small wiggly under ‘from’ in the image below.
I could confirm that PyQt5 was installed. I was able to run the application and I could verify that Visual Studio Code was using the correct Python environment. Why is pylint complaining then?
pylint doesn't load C extensions by default
As it turned out, pylint doesn't load C extension modules by default. Primarily the reason being, it can not get an AST object out of extension module. Another implication is that the extension can run arbitrary code on your system.
So what are your options?
Whitelist C extension
As mentioned in the docs, pylint allows whitelisting the C extension modules. You can add
PyQt5 to the
extension-pkg-whitelist option in the projects
.pylintrc file. If you don't have this file already, it can be created using pylint and edited.
- Run following command in the terminal in the project root dir:
pylint --generate-rcfile > .pylintrc
- This will generate a config file for pylint with the required headers
- Search for
extension-pkg-whitelist=line and append
PyQt5to the line to make it
- If you need to whitelist additional C extensions modules, they can be added in the same list separated by a comma. For example:
In case, you do not want the hassle of passing individual modules, you can change
unsafe-load-any-extension=yes in your
.pylintrc file. This will allow pylint to create AST for all C extensions.
Updating the VS Code pylint options
If you are not running pylint outside VS Code, you can update the pylint options for VS Code.
- Search for setting and choose
Preferences: Open Settings (JSON)
"python.linting.pylintArgs": ["--extension-pkg-whitelist=PyQt5"]to the settings dictionary
In case you want to whitelist any additional C extension modules, they can added after
PyQt5 separated with a comma. For example:
Hope this helps.