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

8

191

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.

Subroutine answered 4/5, 2018 at 14:20 Comment(0)
M
98

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.

Magical answered 21/10, 2022 at 15:12 Comment(4)
This solution was the only one that worked for me. Thanks. Version: 1.77.3. OS: Linux.Weeden
"python.linting.flake8Args" is deprecated; this answer is the working solution.Overtrick
This worked, but it required me to restart VSCodeViki
the only solution that worked for me. Windows 11 and vscode 1.87.0Tipper
S
364

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
Subroutine answered 4/5, 2018 at 14:20 Comment(15)
The file is in /home/<username>/.config/Code/User/settings.json . Or you can navigate there by File>Preferences>Settings and click on any link to "Edit in settings.json" which will open the settings file in VS Code.Stope
code.visualstudio.com/docs/getstarted/…Guay
You can also use the shortcut, synonymous in many apps for the settings shortcut: CMD or CTRL + , and then switch to json view.Rooker
the max-line-length worked for me but I still get xx imported but not used. is there an update to this :DPublishing
Thx the line length error is so annoying :D @dscan why do you want to disable unused import? Why would you import things you dont use? :) If you just dont use it right now but later just comment it out :)Tattoo
I would advise against using this kind of trick if you work on a project that has dedicated configuration files (such as .flake8). Otherwise you will run into incomprehensible configuration conflicts and possibly ignore rules that were purposefully enforced. In this case, use "python.linting.flake8Args": ["--config", ".flake8"] instead.Mier
Sure. If your team has defined specific linting or check style conventions then by all means use that. But if you were annoyed by default PEP style warnings when you were just starting out programming in python, like I was when I wrote this, then this is great.Subroutine
Just to add that you can use the vs code settings UI to do this without editing files yourself. File > Preferences > Settings, then search for "flake8 args". You can change this under "user" or "workspace". For my personal projects I add --ignore=E501 as I don't like line-length warnings at all.Inseverable
Instead of adding this at user level, it is better to add this on workspace level. You just open settings (CMD + , in mac) and select the Workspace tab instead of the User tab. Then in Extensions select the Flake8 add the --max-line-length=120 argument by clicking on the Add Item button.Fictitious
The file on my Mac is in /home/<username>/Library/Application Support/Code/User/settings.jsonEntrust
This solution is DEPRECATED.Newish
concerning the deprecation, see Why does the VS Code Python extension (circa v2018.19) no longer provide support for several Python tools like linters and formatters?Debauched
So how can we do this now?Peatroy
As of Jan/2024 @artBCode's answer is correct (following the deprecation of this answer). I.e. replace "python.linting.flake8Args" with "flake8.args"Leddy
I have changed the correct answer and deprecated my this one.Subroutine
M
98

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.

Magical answered 21/10, 2022 at 15:12 Comment(4)
This solution was the only one that worked for me. Thanks. Version: 1.77.3. OS: Linux.Weeden
"python.linting.flake8Args" is deprecated; this answer is the working solution.Overtrick
This worked, but it required me to restart VSCodeViki
the only solution that worked for me. Windows 11 and vscode 1.87.0Tipper
P
32

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

Publishing answered 11/6, 2020 at 5:5 Comment(0)
E
12

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"
]
Echeverria answered 8/9, 2021 at 15:7 Comment(3)
Only separate args work for me.Mier
You save my day.Disordered
oh cmon! same line args were failing. thanks for saving my day.Calciferous
M
8

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.

Mier answered 18/9, 2021 at 18:59 Comment(3)
flake8 automatically looks for configuration in .flake8 or setup.cfg if no --config is passed.Hoodlum
Yes but flake8 may also use other files that are available. Specifying a file with --config ensures that only this file will be read.Mier
flake8 does not automatically pick a config file when you have several sub-projects (e.g. library + actual project) having each their own config file... And it is still convenient to work from the top-level directory when you need to modify both sub-projects at the same time.Southern
S
4

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

Shrunken answered 6/11, 2023 at 16:24 Comment(0)
D
3

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",
        ]
    }
}
Delorsedelos answered 30/12, 2022 at 3:34 Comment(2)
Thank you! This worked. For some reason setting "python.linting.flake8Args" didn't work in one of my projects, and it was driving me nuts. Setting "flake8.args" did the trick 😄Estellaestelle
This is the right setting identifier by 2023, the other one is marked in the json file as unknown. Thanks for the right and concise answer.Kamenskuralski
A
2

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"],
Andesine answered 27/8, 2023 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.