Under the settings.json
file in VS Code, I've got black
set as the Python formatter:
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.mypyEnabled": true,
"python.linting.lintOnSave": true,
"python.formatting.blackArgs": [
"--line-length",
"120"
],
"python.formatting.autopep8Args": [
"--max-line-length",
"120",
"--in-place"
],
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [
"--max-line-length",
"120",
"--ignore=E501",
]
Black
does a fantastic job formatting Python code, but unfortunately any flake8
errors are not corrected by black
.
So Flake8 errors like Expected 2 blank lines, found 0 (E302)
are not fixed upon saving a .py
file in VS Code.
Fortunately those Flake8 errors CAN be fixed if you run autopep8 --in-place <your_python_script.py>
but obviously this is annoying as it requires an extra manual step.
So the natural question is: how to enable BOTH black
and autopep8 --in-place
to run and trigger upon saving any .py
file?
My understanding is that VS Code has a setting python.formatting.provider
that you can set to black
, or autopep8
, or yapf
, etc. But it only accepts a single formatting provider, and not two.
How to configure VS Code to apply more than one formatter to Python files upon saving those files?
Alternatively, is there a way to trigger autopep8 --in-place
to be triggered upon saving a Python file (in addition to the black formatter)?
note this configuration when working with black you'll want to use black's recommended settings