When using autosave with black and isort in vscode, isort does not work

2.3k Views Asked by At

I want to use autosave in Visual Studio Code and apply commonly used tools like flake8, mypy, isort, and black.

Flake8, mypy, and black work fine, but isort doesn't work at all. Is there a way to solve this problem?

My settings.json file looks like this:

{
  "python.terminal.activateEnvInCurrentTerminal": false,
  "python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe",
  "python.linting.enabled": true,
  "python.linting.lintOnSave": true,
  "python.linting.pylintEnabled": false,
  "python.linting.flake8Enabled": true,
  "python.linting.flake8Path": "${workspaceFolder}\\.venv\\Scripts\\pflake8.exe",
  "python.linting.mypyEnabled": true,
  "python.formatting.provider": "black",
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    },
    "editor.formatOnSave": true
  }
}
3

There are 3 best solutions below

4
ProMine On

You can try adding the following line to your settings.json file:

"python.sortImports.path": "${workspaceFolder}\\.venv\\Scripts\\isort.exe"

After that, save the settings.json file and restart VS Code. Hope this answer can help you

1
Wonee On

I have solved this issue. I did not know that "isort" was a default in vscode, and so I uninstalled it.

I have reinstalled "isort". And I have modified the "settings.json" as follows:

{
  "python.terminal.activateEnvInCurrentTerminal": false,
  "python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe",
  "python.linting.enabled": true,
  "python.linting.lintOnSave": true,
  "python.linting.pylintEnabled": false,
  "python.linting.flake8Enabled": true,
  "python.linting.mypyEnabled": true,
  "python.formatting.provider": "black",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.organizeImports": true,
  },
  "isort.args": [
    "--profile",
    "black"
  ]
}
0
Elijas Dapšauskas On

There's now a new and much better, faster, and more popular alternative now:

with

It is an extremely efficient implementation of isort, black, and other linting tools, available as a single package.

Then, just set up ruff to format your file on auto-save.