Python AutoPep8 格式不适用于最大行长参数

Moh*_*mar 5 python flake8 autopep8 visual-studio-code

我注意到一件奇怪的事情,当我们设置时,VSCode 中的 autopep8 自动格式化不起作用

    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],
Run Code Online (Sandbox Code Playgroud)

但是,如果此设置处于行长 79 的默认模式,则它运行良好。autopep8 是否存在一些问题,仅适用于行长 79 不超过该值,或者我在 VSCode 中犯了任何错误。我需要的主要功能是当我的 python 程序行太长时,它应该能够将它分成多行。我不想继续使用 79 个字符的方法。我的首选方法是 119 个字符。目前,我必须手动缩进大行。除了pep8支持119个字符和自动缩进超过119个字符的行之外,还有没有其他格式。

我附上我的 settings.json 文件数据

{
    "window.zoomLevel": 1,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "diffEditor.ignoreTrimWhitespace": false,
    "editor.fontSize": 16,
    "python.formatting.provider": "autopep8",
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "python.autoComplete.addBrackets": true,
    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],
    // "python.linting.flake8Args": [
    //     "--max-line-length=120"
    // ],
    "files.autoSaveDelay": 10000,
    "editor.defaultFormatter": "ms-python.python",
    "files.autoSave": "afterDelay",
    "files.trimTrailingWhitespace": true,
    "files.trimFinalNewlines": true,
    "editor.quickSuggestions": true,
    "editor.codeActionsOnSave": null,
}
Run Code Online (Sandbox Code Playgroud)

小智 22

实验对我有用

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"]
Run Code Online (Sandbox Code Playgroud)

查看此链接以获取正确的格式说明符设置

  • `--experimental` 对我来说没有必要。 (8认同)
  • 对于 Mac 上的 autopep8 1.5.7,“--experimental”选项对于根据最大长度进行格式化至关重要,否则行将保持在提供的最大长度之上。 (2认同)

Kis*_*n K 12

正如@senti143的回答中提到的,

在 Windows 上,将python自动换行设置为 120 个字符

转到设置并搜索 autopep8

查找 Python > 格式化:Autopep8 Args

单击添加项目按钮并

添加值

--max-line-length
Run Code Online (Sandbox Code Playgroud)

再次单击“添加项目”按钮并添加值

120
Run Code Online (Sandbox Code Playgroud)

再次单击“添加项目”按钮并添加值

--experimental
Run Code Online (Sandbox Code Playgroud)

如下截图所示

在此输入图像描述

现在返回代码并选择必要的代码以将换行格式设置为 120 个字符


小智 7

这应该有效 -

"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
    "--max-line-length",
    "120",
    "--experimental"
]
Run Code Online (Sandbox Code Playgroud)