将 isort 配置移至 vscode settings.json

问题描述 投票:0回答:1

我知道VSCode:如何为Python(isort)配置“组织导入”可能相关,但所有答案似乎都已过时。

我的 pyproject.toml 文件中有以下内容,我的 settings.json 如下所示。虽然

black
似乎运行正常,但如果不手动输入
isort .
,我似乎无法运行 isort。

附带问题,当我通过终端运行 isort 时,它似乎很慢,如果 isort 通常很慢,我是否应该简单地添加一个预提交挂钩?

[tool.black]
line_length = 100

[tool.isort]
honor_noqa = true
line_length = 100
profile = "black"
verbose = false

known_first_party = [
    # all folders you want to lump
  "src",
]

# Block below is google style import formatting https://pycqa.github.io/isort/docs/configuration/profiles.html
force_sort_within_sections = true
force_single_line = true
lexicographical = true
single_line_exclusions = ["typing"]
order_by_type = false
group_by_package = true
{
    "files.insertFinalNewline": true,
    "jupyter.debugJustMyCode": false,
    "editor.formatOnSave": true,
    "editor.formatOnPaste": true,
    "files.autoSave": "onFocusChange",
    "editor.defaultFormatter": "ms-python.black-formatter",
    "black-formatter.path": ["/opt/homebrew/bin/black"],
    "black-formatter.args": ["--config", "./pyproject.toml"],
    "black-formatter.cwd": "${workspaceFolder}",
    "isort.args": ["--config", "./pyproject.toml"],
    "isort.check": true,
    "python.analysis.typeCheckingMode": "basic",
}
python visual-studio-code isort
1个回答
0
投票

如果您希望能够自动排序而不是手动键入命令,您可以将以下代码添加到您的

settings.json

  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": "explicit"
    },
  },
  "isort.args":["--profile", "black"],
© www.soinside.com 2019 - 2024. All rights reserved.