165

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.

reka18
  • 7,440
  • 5
  • 16
  • 37

7 Answers7

345

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
Alex R
  • 11,364
  • 15
  • 100
  • 180
reka18
  • 7,440
  • 5
  • 16
  • 37
  • 16
    The file is in /home//.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. – cryanbhu May 08 '19 at 03:35
  • 2
    https://code.visualstudio.com/docs/getstarted/settings#_settings-file-locations – xverges Jan 16 '20 at 10:40
  • 2
    You can also use the shortcut, synonymous in many apps for the settings shortcut: `CMD` or `CTRL` + `,` and then switch to `json` view. – nosahama May 04 '20 at 04:20
  • the max-line-length worked for me but I still get `xx imported but not used`. is there an update to this :D – dcsan Jun 11 '20 at 05:02
  • 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 :) – chrisaramar Mar 19 '21 at 08:17
  • 1
    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. – Romain Vincent Sep 18 '21 at 18:51
  • 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. – reka18 Sep 18 '21 at 18:53
  • 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. – Owen Jan 08 '23 at 09:48
  • 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. – Karan Sharma Aug 29 '23 at 11:13
36

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.

artBCode
  • 837
  • 10
  • 13
31

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

dcsan
  • 11,333
  • 15
  • 77
  • 118
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"
]
David Newswanger
  • 1,073
  • 1
  • 10
  • 11
7

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.

Romain Vincent
  • 2,875
  • 2
  • 22
  • 29
  • 2
    `flake8` automatically looks for configuration in `.flake8` or `setup.cfg` if no `--config` is passed. – Tim Tisdall Nov 29 '21 at 13:31
  • Yes but flake8 may also use other files that are available. Specifying a file with `--config` ensures that only this file will be read. – Romain Vincent Sep 30 '22 at 18:01
  • 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. – Alexis R Nov 11 '22 at 10:44
1

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",
        ]
    }
}
kvothe__
  • 591
  • 4
  • 10
  • 1
    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 – mcouthon Feb 05 '23 at 12:58
0

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