Python & VS Code: make Black and organize imports work together on save

Hello world. What a stable and positive time to be alive!

But at least we can fix our code editors.

I use Black to format my Python code, and I tell VS Code to organise imports on save.

Here’s the relevant bit of VS Code’s settings.json:

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

It’s a pretty pleasant experience, mostly. But this “flapping” behaviour when I save has been bugging me for weeks:

(It’s even more annoying when you’re part way down a file and the entire screen jerks up and down.)

Clearly, Black and the organize imports thing are fighting. But what is the organize imports thing? And what’s the fix?

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

As a Poetry user, all I had to do is add these lines to the pyproject.toml file at the root of my project:

profile = "black"

Alternatively, create a file called .isort.cfg (note the leading dot) at the root of your project, with this content:


No more flapping, and at least one small corner of the universe is a little more sane.