首页 > 解决方案 > VS Code 和 Python,isort 没有做好它的工作

问题描述

我已经在 VS Code 中安装blackisort用于代码格式化。黑色有效,而isort似乎无效。

如果我从命令行运行isort没有问题。

我试过修改setting.json无济于事。这是最新版本:

{
    "window.zoomLevel": 0,
    /** "editor.codeActionsOnSave": null */
    /** Enable format on save */
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        /** Pyformat use it's own code action to prevent confliction with other tools. */
        "source.organizeImports.pyformat": true,
        "source.organizeImports.python": true
    },
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "python.languageServer": "Pylance",
    /** "python.sortImports.path": "isort", */
    "python.sortImports.args": [
        "-m 3",
        "-tc",
        "-w 88"
    ],
    "python.formatting.provider": "black",
    "python.formatting.blackArgs": [
        "--line-length=88"
    ],
...

有什么建议么?

标签: pythonvisual-studio-codesettingsisort

解决方案


VS Code 一次只支持使用一个格式化程序。但是,此答案中描述了一种解决方法:

const firstFormatter = commands.executeCommand('editor.action.formatDocument');

firstFormatter.then(() => myFormat());

我个人将其配置为使用黑色,然后附加命令以运行第二个。


推荐阅读