VS Code 自动格式化 Python 代码以使用 2 个选项卡

Bre*_*her 5 python visual-studio-code

由于某种原因,Visual Studio Code 看起来正在格式化我的代码以使用 2 个选项卡。我已更改settings.json为使用 2 个空格作为制表符,我已通过按制表符按钮验证了这一点,并且它按预期使用了 2 个空格。然而,Python 缩进仍然使用 4 个空格(或 2 个制表符),如下图所示:

在此输入图像描述

您可以看到它使用两个(2 个间隔)选项卡。我查看了我能找到的所有关于配置 VS Code Python 格式的 Stack Overflow 帖子,但我所做的一切都不起作用。它仍然会自动格式化,看起来像这样。这是我的settings.json

{
  "python.pythonPath": "venv/bin/python3",
  "python.linting.enabled": true,
  "editor.tabSize": 2,
  "editor.formatOnSave": true,
  "editor.detectIndentation": false
}
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么事吗?对于非 Python 文件,它工作正常,所以我不知道这是否是 pylint 的问题。这真让我抓狂。

Bre*_*her 12

我想通了 - 我将格式化程序更改为 autopep8 并告诉它使用 2 个空格。这是我更新的settings.json文件:

{
  "python.pythonPath": "venv/bin/python3",
  "editor.tabSize": 2,
  "editor.formatOnSave": true,
  "python.linting.pylintArgs": [
    "--indent-string='  '"
  ],
  "python.formatting.autopep8Args": [
    "--indent-size=2"
  ],
  "python.linting.pylintEnabled": true,
  "python.formatting.provider": "autopep8"
}
Run Code Online (Sandbox Code Playgroud)

我对这里发生的 linting / 格式化内容有点陌生,但我了解到 linting 和格式化是不同的:https://code.visualstudio.com/docs/python/editing#_formatting。我的 linter 可能告诉我需要两个空格,但我需要将格式化程序配置为在保存文件时使用 2 个空格进行格式化。