How to change the .pylintrc file that VS Code is using for linting?
Asked Answered
S

4

26

We have a .pylintrc file committed to git in our project root that is used by our team which has many useful errors disabled. So VS Code doesn't show those. I want to know if I can use my own pylint rules only on my local machine without having to modify the team's .pylintrc file in git.

I know that pylint has an option called --rcfile which can be used to specify custom .pylintrc file. I think if I can specify this option in VS Code settings I can get it to show me more errors. Do you know how to do it?

Skewer answered 7/1, 2020 at 8:18 Comment(0)
V
17

If you'd rather have the same .pylintrc enabled for all your environments, you can add to your settings.json the command-line pointer to the config:

    "python.linting.pylintArgs": [
        "--rcfile=${env:HOME}/path/to/your/.pylintrc"
    ]

To keep the same config on Windows and Linux I add HOME=%USERPROFILE% to my User Environment variables on Windows.

Vulvitis answered 4/10, 2020 at 23:26 Comment(0)
P
11

You can create the .pylintrc file in the root directory of your workspace workspace/.pylintrc and it should be automatically detected.

Plinth answered 16/6, 2020 at 2:1 Comment(1)
pylintrc (without the dot) is automatically detected by VS Code as wellInevasible
D
6

None of the solutions worked for me. I had to add the following to get it to run:

"pylint.args": [
    "--rcfile=${workspaceFolder}/your/path/.pylintrc"
],
Dreadnought answered 5/6, 2023 at 10:26 Comment(0)
F
1

TL;DR Using WSL and VSCode I had to change settings.json

{
    // whatever I had in settings json before plus:
    "python.linting.pylintArgs": [
        "--rcfile", "~/projects/my-project/.pylintrc"
    ]
}

and then

pylint --generate-rcfile > .pylintrc

and then I could add for example missing-function-docstring to my disables.

The full story:

I wanted the default VSCode pylinting in my code base for version control, and to be able to ignore specific errors.

I had so much trouble getting this to work because there are no errors if you specify the wrong path for --rcfile it just doesn't lint. And Ctrl-Shift-P -> Run linter doesn't seem to work so I had to restart VSCode everytime to check if it was working. Anyway, I finally got it working.

Here's what I had to do running VSCode on files on WSL:

  1. Similar to the top voted answer, I added python.linting.pylintArgs to settings.json (Ctrl-Shift-P -> Preferences: Open User Settings (JSON))
"python.linting.pylintArgs": [
    "--rcfile", "~/projects/my-project/.pylintrc"
]

Examples of what didn't work as a path and silently errored: \\wsl$\Debian\home\matthias\projects\my-project\.pylintrc and \\\\wsl$\\Debian\\home\\matthias\\projects\\my-project\\.pylintrc

  1. I had to realize .pylintrc files are not json but config file format, something like
[MASTER]

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=

# Specify a score threshold to be exceeded before program exits with error.
fail-under=10.0

# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=CVS

...

Then I found some .pylint file online (and if there's an error it just silently doesn't work), but even though it did work it didn't have the options that come default with VSCode, so I had to follow the instructions here and install pylint (sudo apt install pylint) and open a terminal in VScode and run

~/projects/my-project$ pylint --generate-rcfile > .pylintrc

Then I can add specific errors to the disable line in [MESSAGES].

Lessons I learned later:

  • It is enough to close and reopen the file to get the linting going again.

Extra information:

  • If you don't care about having the .pylintrc file in version control, you can also disable rules directly in the settings.json file, e.g.
"python.linting.pylintArgs": [
    "--disable", "C0116"
]
Flan answered 5/5, 2023 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.