Python 自动格式化添加额外空格

Python Auto-Formatting Adds Extra Spaces

我一直面临的问题与我的 Python 文件的 Visual Studio 代码中的自动格式化有关。

我喜欢在 Python 中使用制表符,因为它更容易保持一致并更快地键入代码;但是,当我保存 Visual Studio 代码时,保存时的自动格式会向每一行添加一个额外的 space。这意味着 python 脚本有效,但结构看起来不对。

我试过禁用美化,但它仍然发生。我不认为更漂亮的自动格式 python 无论如何。我尝试检查设置 JSON 文件,但我认为那里也没有任何内容。

Settings.json:

{
    "color-highlight.markerType": "dot-before",
    "editor.detectIndentation": false,
    "editor.formatOnSave": true,
    "editor.tabSize": 3,
    "liveServer.settings.donotShowInfoMsg": true,
    "prettier.tabWidth": 3,
    "python.pythonPath": "C:\Users\mtapi\Anaconda3\python.exe",
    "window.zoomLevel": 1,
    "python.condaPath": "C:\Users\mtapi\Anaconda3\Scripts\conda.exe",
    "terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
    "editor.insertSpaces": false,
    "prettier.useTabs": true,
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.linting.pep8Enabled": true,
    "editor.mouseWheelZoom": true,
    "editor.fontSize": 15,
    "workbench.iconTheme": "vscode-icons",
    "workbench.colorTheme": "Default High Contrast"
}

这是一个示例脚本在保存格式之前:

这是保存后发生的情况:

这在以前没有发生过。让我知道是否有任何混淆或需要更多信息

您的编辑器中的某些内容似乎配置为有 4 个空格而不是 3 个缩进。

但是,如果您想遵循 PEP8,我建议您使用 4 个空格作为缩进大小,正如提案本身所建议的(而不是制表符):https://www.python.org/dev/peps/pep-0008/#indentation

话虽如此,我猜这与这些行有关,因为它们告诉编辑器使用 PEP8,而 PEP8 要求 4 个空格:

    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.linting.pep8Enabled": true,

最后,我注意到的最后一件事:您的 'before' 脚本的缩进大小不一致,行 print(f'{A} {B} {C} {D}') 的缩进大小与代码的其余部分不同。