16

Mirror question to:

I want to configure how VSCode is invoking isort, so I can customize when calling Organize imports in a .py file.


In particular, VSCode has started removing a blank line between two isort-sections, don't know why.

from django...
from myproject... # removing blanck line between 2 sections
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
  • 1
    It seems that this might be helpful: https://medium.com/@cereblanco/setup-black-and-isort-in-vscode-514804590bf9 , you customize by altering the `"python.sortImports.args"`. Another alternative that I am using is the `pre-commit` with `isort` – Dimitar Apr 12 '21 at 13:57
  • When tried those, a pop-up appear: "following deprecated CLI flags were used and ignored: -rc!" – Manu Artero Apr 12 '21 at 14:09
  • I think the recursive option is deprecated since it is done automatically. https://github.com/PyCQA/isort/issues/1263 – Dimitar Apr 12 '21 at 14:27

5 Answers5

16

You can make isort compatible with black and enjoy a formatted code upon saving your document. Here are two ways to achieve this:

  1. You can configure Black and Isort’s settings on pyproject.toml. Inside your root’s project folder, create/update your pyproject.toml file:
[tool.isort]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
line_length = 88
profile = "black"
  1. Edit settings.json and configure Black and Isort.
{
   "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
       }
     },

   "python.formatting.provider": "black",
   "isort.args": ["--profile", "black"],
}

source.organizeImports: true runs Isort automatically upon saving your document.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
xshapira
  • 355
  • 4
  • 13
  • Hey @xshapira do you need any extensions to make VSCode read that config from `pyproject.toml` file? – jkulak Nov 05 '22 at 18:27
  • @jkulak You need `poetry` as your package manager and `pip install isort`. – xshapira Nov 08 '22 at 10:14
  • Do you need any VSCode extensions for `isort` to read config from `pyproject.toml`? – jkulak Nov 09 '22 at 14:43
  • 1
    @jkulak I think this used to work automatically in older versions of VSCode (or the Python extensions), but with the latest release it seems to have stopped working. I've opened an [issue here](https://github.com/microsoft/vscode-python/issues/20205). – bluenote10 Nov 10 '22 at 16:19
  • Thx. I have a question about that https://stackoverflow.com/questions/74330561/how-to-make-vscode-readisort-config-from-a-file And also I am looking at that issue: https://github.com/microsoft/vscode-isort/issues/71 – jkulak Nov 10 '22 at 21:21
  • Now we need to install [ms-python.isort](https://marketplace.visualstudio.com/items?itemName=ms-python.isort) – Noam Nol May 07 '23 at 12:06
12

As of the end of 2022, the python.sortImports setting from the vscode-python will be removed. See link.

VS Code wants us to use isort.args from the vscode-isort instead. For example, the common configuration

   "python.sortImports.args": ["--profile", "black"],

should be replaced with

   "isort.args": ["--profile", "black"],

Otherwise you will see the

"This setting will be removed soon. Use isort.args instead."

message.

MrE
  • 19,584
  • 12
  • 87
  • 105
Pawel Kam
  • 1,684
  • 3
  • 14
  • 30
  • If you also want to use `black` as a formatter, you will additionally need to install `ms-python.black-formatter` extension, then set your default formater for python to be `black` – smac89 Feb 27 '23 at 07:48
  • I needed to add `"source.organizeImports": true`, otherwise it only checked `isort` upon save, it did not apply `isort`. (See https://stackoverflow.com/questions/67059648/vscode-how-to-config-organize-imports-for-python-isort/72667901#72667901 ) – wisbucky May 23 '23 at 09:44
8

In VS Code, the "Python" extension provides us with the following settings, which can merge specific imports from the same module into a single import statement, and organize the import statements in alphabetical order. (in "settings.json" file)

"python.sortImports.args": ["-rc", "--atomic"],

For using "Sort Imports" in VS Code, please refer to this document: Sort Imports in VS Code.

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • do you know which flags to use for the "expected" order (one that doesn't lead to a `pylint-wrong-import` error) – Manu Artero Apr 13 '21 at 07:26
  • @Manu -I'm sorry I don't know about this, but you could create a new thread and describe this question in detail to get a better answer. – Jill Cheng Apr 13 '21 at 07:32
1

You can install Isort extension to VSCode as shown below. *I use Anaconda on Windows 11:

enter image description here

Then, set the code below to settings.json. *You can see my answer explaning how to open settings.json:

// "settings.json"

"[python]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

This below is my full settings.json with the code above:

// "settings.json"

{
    "python.defaultInterpreterPath": "C:\\Users\\kai\\anaconda3\\python.exe",
    "window.zoomLevel": 3,
    "files.autoSave": "afterDelay",
    "breadcrumbs.enabled": false,
    "[python]": { // Here
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    }
}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

See my answer in How to find which isort is vscode exactly using where I describe the steps to see what is actually happening. You will see the command along with the arguments. The version of isort that is built in to the Microsoft Python plugin doesn't show as isort. Instead it is sortImports.py. It has the path so you can go look at the code for more information.

julie
  • 172
  • 1
  • 6