我使用以下方法安装了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
问题出在错误的设置中。要使用yapf,black或autopep8,您需要:
.vscode/settings.json
:文件的一部分:
{
"python.linting.enabled": true,
"python.linting.pylintPath": "pylint",
"editor.formatOnSave": true,
"python.formatting.provider": "black",
"python.linting.pylintEnabled": true,
}
关键选项-"editor.formatOnSave": true,
,这意味着yapf
每次保存时都会格式化您的文档。