首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VSCode中使用yapf (或black)

如何在VSCode中使用yapf (或black)
EN

Stack Overflow用户
提问于 2020-01-20 18:45:05
回答 3查看 17.5K关注 0票数 15

我使用以下命令安装了yapf:

代码语言:javascript
复制
conda install yapf

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

代码语言:javascript
复制
{
    //"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,
}

但我无法理解如何使用它-它不会在格式错误的脚本中显示任何错误:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-02-04 18:33:43

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

black)

  • Configure

  • 安装yapf // autopep8 (通过下一种方式安装yapf/
  1. /autopep8:

文件的一部分:

代码语言:javascript
复制
{
    "python.linting.enabled": true,
    "python.linting.pylintPath": "pylint",
    "editor.formatOnSave": true,
    "python.formatting.provider": "yapf", // or "black" here
    "python.linting.pylintEnabled": true,
}

Key option - "editor.formatOnSave": true,这意味着每次保存文档时yapf都会格式化文档。

票数 21
EN

Stack Overflow用户

发布于 2021-02-26 06:06:17

扩展@Mikhail_Sam应答。您可能希望按照我的喜好使用单独的配置文件。通过这种方式,您可以将项目设置从VS Code IDE中分离出来。为此,您需要创建.style.yapf

代码语言:javascript
复制
type null > .style.yapf   (for windows environment)
touch .style.yapf    (for MacOS, Linux environments)

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

代码语言:javascript
复制
[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

代码语言:javascript
复制
"python.formatting.yapfArgs": [
  "--style={based_on_style: google, column_limit: 80, indent_width: 2}"
],

我在settings.json中的其他VS代码设置

代码语言:javascript
复制
"[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将通过以下方式搜索格式化样式:

在命令行上指定的yapf样式VS代码样式当前目录或其父目录中的directories.

  • In文件的

  • 部分当前目录或其父目录中的>>文件的yapf部分您的家庭directory.

  • If中的~/.config/yapf/文件的
  1. 部分未找到这些文件,将使用默认样式(PEP8)。
票数 9
EN

Stack Overflow用户

发布于 2022-02-11 10:18:26

2022年的答案

如果您喜欢'GUI',您也可以直接在设置(文件->首选项->设置)下输入这些值:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59821618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档