且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Visual Studio Code 中自动缩进 Python 代码?

更新时间:2022-12-19 09:49:29

在 VS Code 中你可以在几个地方设置缩进:

In VS Code you can set the indentation in several places :

  • 常规/工作区设置(底部栏),
  • 用户设置,
  • 语言格式化程序设置.

使用 Python 时,无论您设置什么设置,所有设置都会被 autopep8 语言格式化程序设置的 autopep8Args 值覆盖,该设置的缩进大小为4.

When using Python, no matter what settings you set, all of them are overridden by the autopep8Args value of the autopep8 language formatter setting, which has an indent size of 4.

默认情况下,autopep8 用作 VS Code Python 格式化程序,但还有其他格式,例如 yapf.

By default, autopep8 is used as VS Code Python formatter, but there are others, like yapf.

要更新此格式化程序的缩进大小,请在您的用户设置中搜索python.formatting.autopep8Args"并将其设置为:["--indent-size=2"],

To update the indent size of this formatter, search in your user settings the "python.formatting.autopep8Args" and set it to : ["--indent-size=2"],

"python.formatting.autopep8Args": ["--indent-size=2"],