How to use Black to format Python code, and organise imports on save in VSCode.

Here’s the relevant bit of VSCode’s settings.json:

{
    "python.formatting.provider": "black",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

It turns out that VS Code uses the isort library to sort imports, and isort has a Black compatability profile.

If you use Poetry, add these lines to the pyproject.toml file at the root of the project:

[tool.isort]
profile = "black"

Alternatively, create a file called .isort.cfg at the root of the project, with this content:

[settings]
profile=black

Comments

comments powered by Disqus