I met strange behavior of pylint in VS Code. ‘.pylintrc’ doesn’t recognize after recreating.
My steps:
-
Install pylint in VS Code
-
Set pylint as linter using ‘Python: Select linter’ command
-
Add ‘.pylint’ with disabling some of warnings:
[MESSAGES CONTROL] disable=missing-function-docstring,
missing-final-newline,
missing-class-docstring,
missing-module-docstring,
invalid-name,
too-few-public-methods
And it works fine! But then I tried to set pylint configuration in ‘pyproject.toml’:
[tool.pylint.messages_control]
disable = ["missing-function-docstring",
"missing-final-newline",
"missing-class-docstring",
"missing-module-docstring",
"invalid-name",
"too-few-public-methods"
]
After that exluded warnings shows again.
Ok, I deleted ‘pyproject.toml’ and return ‘.pylintrc’ – no effect. I tried to select linter again, reopen VS Code, reinstall pylint, but nothing helps.
Version: 1.70.0 (user setup)
Commit: da76f93349a72022ca4670c1b84860304616aaa2
Date: 2022-08-04T04:38:16.462Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Windows_NT x64 10.0.19044
pylint version 2.15.0
2
Answers
Two things:
Configuring pylint via
pyproject.toml
is indeed a hassle, because this specific file only gets respected if pylint is run from precisely that directory. Many IDEs however (Spyder at least, but it seems PyCharm is similar) apparently always run pylint from the respective subdirectory, i.e. from the folder that directly contains the file undergoing linting. Thus, yourpyproject.toml
isn’t respected in these cases. (If you find this behavior weird, you’re not the only one, but that’s how pylint currently does it.) So this is the underlying reason for the errors in your screenshot. On the other hand, any.pylintrc
in the project does indeed get respected even when pylint is run from inside a subdirectory. So for now I would indeed recommend configuring pylint by a.pylintrc
in your project and not bypyproject.toml
. (I think there is some option whereby one can make.pylintrc
just point to thepyproject.toml
, which would then hold all the concrete configuration and would be respected even from subdirectories – this would then be the best solution for the time being.)To resolve your issue that remains with
.pylintrc
: Tell pylint to show you the path of the configuration file which it uses when it’s called. For this, use the command line:cd
into the directory in question and run pylint in verbose mode:pylint --verbose
. This shows you what config file is being used. (For instance, you might have a long-forgottenpylintrc
or.pylintrc
sitting around somewhere in your home directory that gets loaded.)By the way, this trick – using verbose mode to see what configuration file is actually being used – is useful with many tools.
Maybe a bit too late for OP, but probably interesting for others:
I struggeled with the same problem, but using the locally installed
pylint
via Pipfile in a virtual environment (venv
). It seems the pylint command line parameterrcfile
solves this. Insettings.json
writeif you had the following file structure
where the Python project, including
pyproject.toml
, is inside the subdirectoryapi
Note: this works likewise with a
.pylintrc
file by writing--rcfile=api/.pylintrc
to thesettings.json
instead