Mik*_*Sam 14 python intellisense pylint visual-studio-code yapf
我使用以下方法安装了 yapf:
conda install yapf
Run Code Online (Sandbox Code Playgroud)
并在我的.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,
}
Run Code Online (Sandbox Code Playgroud)
但我不明白如何使用它 - 它在格式错误的脚本中没有显示任何错误:
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
Run Code Online (Sandbox Code Playgroud)
Mik*_*Sam 19
问题出在错误的设置中。要使用 yapf、black 或 autopep8,您需要:
.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,
}
Run Code Online (Sandbox Code Playgroud)
关键选项 -"editor.formatOnSave": true,
这意味着yapf
每次保存文档时都要格式化文档。
延伸@Mikhail_Sam
回答。您可能想根据需要使用单独的配置文件。通过这种方式,您可以将项目设置与 VS Code IDE 分离。为此,您需要创建.style.yapf
:
type null > .style.yapf (for windows environment)
touch .style.yapf (for MacOS, Linux environments)
Run Code Online (Sandbox Code Playgroud)
向 中添加规则.style.yapf
,例如:
[style]
based_on_style = google
spaces_before_comment = 4
indent_width: 2
split_before_logical_operator = true
column_limit = 80
Run Code Online (Sandbox Code Playgroud)
不要忘记从 VS 代码中删除settings.json
以下设置。他们覆盖.style.yapf
:
"python.formatting.yapfArgs": [
"--style={based_on_style: google, column_limit: 80, indent_width: 2}"
],
Run Code Online (Sandbox Code Playgroud)
我的其他 VS Code 设置settings.json
:
"[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,
Run Code Online (Sandbox Code Playgroud)
根据YAPF 文档:YAPF 将按以下方式搜索格式样式: