Manu Artero
Manu Artero

Reputation: 10253

VSCode: how to config 'Organize imports' for Python (isort)

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

Upvotes: 25

Views: 43222

Answers (6)

xshapira
xshapira

Reputation: 582

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.

As of version 1.85, you want to set "source.organizeImports": "explicit" to get the same functionality as the old "true", per the docs.

Upvotes: 31

Kirk Walla
Kirk Walla

Reputation: 381

In my case isort is not sorting the way I'd like to to be sorted. Here's MWE:

import numpy as np
import pandas as pd
import seaborn as sea_bor_nnnn
import matplotlib.pyplot as plt
import torch
import os

after saving the file I get the following output

import os
                            # why is it inserting an empty line here?
import numpy as np
import torch
import pandas as pd
import seaborn as sea_bor_nnnn
import matplotlib.pyplot as plt
                               # here inserts only 1 blank line instead of 2
#actual code goes here
dfdf
dfdfd
ddf

My preferred format would be the following

import os
import torch
import numpy as np
import pandas as pd
import seaborn as sea_bor_nnnn
import matplotlib.pyplot as plt


# actual code goes here

Notice how all imports are sorted according to length without empty lines in between and that actual code starts after 2 empty lines from the module imports.

Any ideas how to achieve that in vscode? Maybe using using isort in combination with autopep8 or flake8 instead of black

Upvotes: 0

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
        }
    }
}

Upvotes: 5

Pawel Kam
Pawel Kam

Reputation: 2134

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.

Upvotes: 23

julie
julie

Reputation: 323

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.

Upvotes: 0

Jill Cheng
Jill Cheng

Reputation: 10354

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.

Upvotes: 8

Related Questions