-1

Here is my settings.json:

{
    "python.pythonPath": "/home/zhaodachuan/anaconda3/envs/ranking_engine/bin/python",
    "python.autoComplete.extraPaths": [
        "/mnt/c/Users/hnjyz/OneDrive/jupyter_lab/code/ranking_engine",
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120",
        "--ignore=E402,F401",
    ],
}

But the flake8 --ignore doesn't work when I turn on the format on save in VS Code.

What should I do?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34

1 Answers1

1

Flake8 is not a formatter, it's a linter.

It's certainly one of the supported linters of VS Code's Python extension, but it will not affect the setting editor.formatOnSave or be triggered by the Format Document command. If configured correctly, it should automatically run and check your file for possible issues, then display them on the Problems tab:

sample Flake8

If I add:

    "python.linting.flake8Args": [
        "--ignore=F401"
    ],

Then the previously displayed F401 error should disappear:

sample Flake8 with ignore

What you seem to be looking for is a formatter. See the Formatting section of the VS Code docs on Python: https://code.visualstudio.com/docs/python/editing#_formatting:

Formatting makes code easier to read by human beings by applying specific rules and conventions for line spacing, indents, spacing around operators, and so on (see an example on the autopep8 page). Formatting doesn't affect the functionality of the code itself. (Linting, on the other hand, analyzes code for common syntactical, stylistic, and functional errors as well as unconventional programming practices that can lead to errors. Although there is a little overlap between formatting and linting, the two capabilities are complementary.)

The Python extension supports source code formatting using either autopep8 (the default), black, or yapf.

Install one of the selected formatters, and enable formatOnSave:

    "[python]": {
        "editor.formatOnSave": true
    },
    "python.formatting.provider": "yapf",
    "python.formatting.yapfPath": "/usr/local/bin/yapf",
    "python.formatting.yapfArgs": [
        "--style=/path/to/setup.cfg"
    ],

enter image description here

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135