reka18
reka18

Reputation: 8427

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

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.

Upvotes: 200

Views: 146526

Answers (8)

reka18
reka18

Reputation: 8427

NOTE THAT HIS ANSWER HAS BEEN DEPRECATED! THANKS TO ALL WHO UP-VOTED! I'VE MOVED THE CHECKMARK TO THE BEST ANSWER AS OF MARCH 2024.

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

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

Legend:

  • E402: Module level import not at top of file
  • F841: Local variable is assigned to but never used
  • F401: Module imported but unused
  • E302: Expected 2 blank lines, found 0
  • E305: Expected 2 blank lines after class or function definition, found 1

Upvotes: 374

phenderbender
phenderbender

Reputation: 785

Navigate to file > settings and add it as an argument directly like this enter image description here

Upvotes: 5

Marcelo Galli
Marcelo Galli

Reputation: 41

Following the new rules, you have to do the installation first:

pip install flake8

Add a .flake8 file to the root of the project, this file can have this model:

[flake8]
ignore = E226,E302,E41
max-line-length = 88
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist

And after that add this line to the settings.json file inside the .vscode folder:

"flake8.args": ["--config=.flake8"],

Upvotes: 4

kvothe__
kvothe__

Reputation: 661

To extend (change) the default Flake8 line length I added the following in my VS Code workspace: project.code-workspace:

{
    ...
    "settings": {
        "flake8.args": [
            "--max-line-length=120",
        ]
    }
}

Upvotes: 5

artBCode
artBCode

Reputation: 1942

In my case (vscode 1.72.2, flake 5.0.4) it only worked by adding:

"flake8.args": [
    "--max-line-length=120"
 ]

in the settings.json

I prefer editing the Workspace settings, namely <root project folder>/.vscode/settings.json, because I store it in version control. This way it is backed up and everyone working on the project will get it.

What was suggested in most of the other answers:

"python.linting.flake8Args": [
   "--max-line-length=120",
],

had no effect for me.

Upvotes: 142

Romain Vincent
Romain Vincent

Reputation: 3581

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

Using --config .flake8 ensures only this file will be read (See official doc). So it is important to use this option, even though it is a default value. Otherwise, a custom user configuration in a parent folder could accidentally be used.

Upvotes: 8

David Newswanger
David Newswanger

Reputation: 1083

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

Upvotes: 12

dcsan
dcsan

Reputation: 12315

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

Upvotes: 32

Related Questions