How do I get flake8 to reliably ignore rules in VS Code?

Visual Studio-CodeFlake8

Visual Studio-Code Problem Overview


Two things that annoy me. First is the warning Flake8 gives me when I type more than 80 characters on a line. Second is the warnings I get when I haven't yet used a module name that I imported. I've looked at all the documentation on using Flake8 in the terminal. No use.

flake8 --ignore=E402
flake8 --max-line-length=120

This doesn't work. At least VS Code doesn't show any effect.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Add your arguments to your USER SETTINGS json file like this:

"python.linting.flake8Args": [
    "--max-line-length=120",
    "--ignore=E402,F841,F401,E302,E305",
],

Solution 2 - Visual Studio-Code

note that flake8 uses

"python.linting.flake8Args": [

whereas black seems to use:

"python.formatting.blackArgs": [

if you're using both (or toggling) these settings maybe helpful:

    {
        "python.linting.pylintEnabled": false,
        "python.linting.flake8Enabled": true,
        "python.linting.enabled": true,
        "python.formatting.provider": "black",
        "python.formatting.blackArgs": [
            "--line-length",
            "120"
        ],
        
        "python.linting.flake8Args": [
            "--max-line-length=120",
            "--ignore=E402",
        ],
    
        "python.pythonPath": "venv/bin/python"
    }

Solution 3 - Visual Studio-Code

I ran into this problem recently. I ran into problems because I was setting the argument to --config flake8.cfg instead of --config=flake8.cfg. Under the hood, vscode puts the CLI argument in quotes. Adding "--config flake8.cfg" to the flake8 command seems to confuse flake8 into thinking that it's looking at a file path and not a CLI argument.

The solution for me was to either set the args as --config=flake8.cfg (with the equals sign) or the args up into separate items in the array:

"python.linting.flake8Args": [
  "--config",
  "flake8.cfg"
]

Solution 4 - Visual Studio-Code

The solution proposed by reka18 is great and was no doubt written specifically for the original question.

From a more general stand point, I would advise against using this kind of trick if you work on a project that has dedicated configuration files.

You are guaranteed to run into incomprehensible configuration conflicts and will possibly ignore rules that were purposefully enforced by the project.

In this case, you should use the following instead:

> assuming the file is named .flake8 and is present at the project's root folder

// .vscode/settings.json
"python.linting.flake8Args": ["--config", ".flake8"],

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionreka18View Question on Stackoverflow
Solution 1 - Visual Studio-Codereka18View Answer on Stackoverflow
Solution 2 - Visual Studio-CodedcsanView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeDavid NewswangerView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeRomain VincentView Answer on Stackoverflow