我使用以下命令安装了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发布于 2020-02-04 18:33:43
问题出在错误的设置中。要使用yapf、black或autopep8,您需要:
black)
文件的一部分:
{
"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都会格式化文档。
发布于 2021-02-26 06:06:17
扩展@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代码设置
"[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.
发布于 2022-02-11 10:18:26
2022年的答案
如果您喜欢'GUI',您也可以直接在设置(文件->首选项->设置)下输入这些值:

https://stackoverflow.com/questions/59821618
复制相似问题