出于某种原因,Visual Studio代码看起来好像让我的代码使用了两个选项卡。我已经更改了我的< code>settings.json,使用了2个空格作为制表符,我已经通过按tab按钮验证了这一点,它使用了2个空格,正如预期的那样。然而,4个空格(或2个制表符)仍然用于Python缩进,正如您在这张图片中看到的:
您可以看到它使用了两个(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
}
有人知道是怎么回事吗?对于非Python文件,它工作得很好,所以我不知道这是否是pylint的问题。这快把我逼疯了。
我想通了-我将格式化程序更改为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"
}
我对这里发生的棉绒/格式化事情有点陌生,但我了解到棉绒和格式化是不同的:https://code.visualstudio.com/docs/python/editing#_formatting.我的棉绒可能会告诉我我需要两个空格,但当我保存文件时,我需要将格式化程序配置为用两个空格格式化。
这也可以通过以下步骤实现
查看-