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

问题描述 投票:0回答:1

我遇到的问题与我的Python文件的Visual Studio代码中的自动格式化有关。

我喜欢在Python中使用选项卡,因为它可以更容易地保持一致并更快地输入代码;但是,当我保存在Visual Studio代码上时,自动格式保存会为每一行添加额外的空间。这意味着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"
}

以下是保存格式之前的示例脚本:

enter image description here

以下是保存后会发生的事情:

enter image description here

This did not happen before. Let me know if anything is confusing or more information is needed

python visual-studio-code pep8 autopep8
1个回答
0
投票

看起来编辑器中的某些内容被配置为有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}')与其余代码具有不同的缩进大小。

© www.soinside.com 2019 - 2024. All rights reserved.