【问题标题】:How to use yapf (or black) in VSCode如何在 VSCode 中使用 yapf(或 black)
【发布时间】:2020-05-06 08:44:20
【问题描述】:

我使用以下方式安装了 yapf:

conda install yapf

并在我的.vscode/settings.json 文件中添加下一行:

{
    //"python.linting.pylintEnabled": true,
    //"python.linting.pycodestyleEnabled": false,
    //"python.linting.flake8Enabled": true,
    "python.formatting.provider": "yapf",
    "python.formatting.yapfArgs": [
        " — style",
        "{based_on_style: pep8, indent_width: 4}"
    ],
    "python.linting.enabled": true,
}

但我不明白如何使用它 - 它不会在格式错误的脚本中显示任何错误:

import pandas as pd

class MyClass(object):
    def __init__(self, some_value: int):
        self.value = some_value
    def one_more_function(self, another_value):
        print(another_value)
myObject = MyClass(45)
myObject.one_more_function(2)
my__object2 = MyClass(324)

    print('ok')
def some_foo():
    """
    """
    pass

【问题讨论】:

    标签: python visual-studio-code intellisense pylint yapf


    【解决方案1】:

    问题出在错误的设置中。 要使用 yapf、black 或 autopep8,您需要:

    1. 安装 yapf / black / autopep8 (pip install black)
    2. 接下来配置.vscode/settings.json

    部分文件:

    {
        "python.linting.enabled": true,
        "python.linting.pylintPath": "pylint",
        "editor.formatOnSave": true,
        "python.formatting.provider": "yapf", // or "black" here
        "python.linting.pylintEnabled": true,
    }
    

    密钥选项 - "editor.formatOnSave": true, 这意味着 yapf 每次保存文档时都会对其进行格式化。

    【讨论】:

    • 关于yapfArgs,应该这样输入"python.formatting.yapfArgs": ["--style={based_on_style: pep8, indent_width: 4}"],
    • 我同意关键解决方案是您需要 "editor.formatOnSave": true,但要指出您的问题是使用 yapf 是 vscode,然后您的解决方案使用黑色,一种不同的自动格式化程序。很高兴您以适合您的方式在保存时自动格式化。黑色也很棒。
    • @MaxPower 嗨!你是对的,看起来我尝试了黑色然后发布答案:) 我想,将“黑色”更改为“yapf”应该可以。
    • 这对我不起作用(在 Linux 上)。 Ctrl + Shift + I 格式,但不使用我定义的规则。例如,使用"python.formatting.yapfArgs": ["--style={column_limit: 80}"], 会在第一个( 处中断一条长行,但新创建的缩进行超过了80 个字符的标记。我想不出任何可以禁止我的预期行为的扩展。
    • 我还尝试在~/.config/yapf/style 定义一个配置文件,它允许在命令行中正确格式化文件,但不能在 VS Code 中。省略上面的settings.json 行无效。
    【解决方案2】:

    扩展@Mikhail_Sam 答案。您可能想根据我的喜好使用单独的配置文件。这样,您就可以将项目设置与 VS Code IDE 分离。为此,您需要创建 .style.yapf:

    type null > .style.yapf   (for windows environment)
    touch .style.yapf    (for MacOS, Linux environments)
    

    .style.yapf添加规则,例如:

    [style]
    based_on_style = google
    spaces_before_comment = 4
    indent_width: 2
    split_before_logical_operator = true
    column_limit = 80
    

    不要忘记从您的 VS 代码中删除 settings.json 以下设置。他们覆盖.style.yapf

    "python.formatting.yapfArgs": [
      "--style={based_on_style: google, column_limit: 80, indent_width: 2}"
    ],
    

    我在settings.json中的其他VS Code设置:

    "[python]": {
      "editor.defaultFormatter": "ms-python.python",
      "editor.formatOnSave": true
    },
    "python.formatting.provider": "yapf",
    "python.formatting.yapfPath": "C:\\ProgramData\\envCondaPy379\\Scripts\\yapf.exe",
    "python.formatting.blackPath": "C:\\ProgramData\\envCondaPy379\\Scripts\\black.exe",
    "python.linting.lintOnSave": true,
    "python.linting.enabled": true,
    "python.linting.pylintPath": "pylint",
    "python.linting.pylintEnabled": true,
    

    根据YAPF documentation:YAPF 会按照以下方式搜索格式化样式:

    1. 在命令行中指定 >> VS Code settings.json
    2. 在 .style.yapf 文件的 [style] 部分中的任一当前 目录或其父目录之一。
    3. 在当前目录或其父目录之一的 setup.cfg 文件的 [yapf] 部分中。
    4. 在您的主目录中 ~/.config/yapf/style 文件的 [style] 部分中。
    5. 如果没有找到这些文件,则使用默认样式 (PEP8)。

    【讨论】:

    • 这对我很有帮助。有没有办法在保存之前查看会更改的内容?我更喜欢自己进行更改,以便我习惯于一开始就正确编写它,而不是依赖于保存时执行它的格式化程序......
    • 这件事我没研究过
    • 我的坏了,因为 linter 在我安装 pylint 并告诉它使用 pylint 之前无法工作。感谢您的帮助。
    • @DieterVansteenwegenON4DD 在 VS 代码编辑器中,您可以单击右键并从上下文菜单“格式化文档”中选择,或者如果您选择了一些代码“格式选择”菜单选项。这两个命令将使用“yapf”设置格式化代码。您可以通过按 Ctrl + Z 取消格式化
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2018-06-21
    • 2022-06-21
    • 1970-01-01
    相关资源
    最近更新 更多