如何使用 VSCode 配置 Pylint?

Kev*_*ang 7 python pylint visual-studio-code

对于我的设置,我有 Pylint ==3.0.0a4 和 VSCode 版本:1.67.2。在我的 VSCode settings.json 中,我有

"editor.formatOnSave": true,
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
Run Code Online (Sandbox Code Playgroud)

每次我保存 .py 文件时,VSCode 都会运行以下命令:

~/.local/share/virtualenvs/hello-world-n4ICiflw/bin/python ~/.vscode/extensions/ms-python.python-2022.6.2/pythonFiles/linter.py -m pylint ~/code/hello-world/src/main.py
Run Code Online (Sandbox Code Playgroud)

我相信 VSCode 通过代理调用运行 Python linter默认情况下linter.py -m pylint使用--output-format=json,但它失败并出现以下错误

##########Linting Output - pylint##########


[ERROR 2022-4-30 15:11:59.266]: Linter 'pylint' failed to parse the output '. [SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at s.parseMessages (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:518074)
    at s.run (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:499508)
    at async s.runLinter (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:517589)]
Run Code Online (Sandbox Code Playgroud)

看起来好像linter.py无法处理 pylint 生成的 JSON 输出。

我也尝试安装 Pylint 扩展,但遇到了同样的问题。

在 shell 上手动运行$ pylint src/main.py效果很好。

还有其他人遇到过这个问题并解决了吗?

Kev*_*ang 2

通过将 pylint 降级到 ==2.13.4 解决了这个问题。似乎这是Pylint 最新版本中的一个已知问题。