使用 VSCode 实时检查 Python

问题描述 投票:0回答:2

最近我使用 VSCode 作为我的 Python IDE,并且安装 DonJayamanne/pythonVSCode,支持 linting。然而,linter 仅在保存时起作用,但我想要的是实时 linting作者建议的解决方法是将

files.autoSave
设置为
on
,这样只要文件自动保存,linter就会工作。 Github 上有相关讨论,供大家参考。

由于我不想打开自动保存功能,有什么办法可以用 VSCode 对 Python 进行实时 linting 吗?或者有什么建议的扩展吗?

visual-studio-code editor
2个回答
0
投票

如果您使用 shift + cmd + P(或 ^+ctrl+P 对于 Windows)或转到“视图”>“命令面板”并输入“Lint”

命令面板允许您执行不同的命令,在这里您可以启用/禁用 Linting 并选择您想要使用的 Linter。最流行的是 PyLint,但您可以选择 Flake8 或 Pep8 或任何您喜欢的。

我相信在 linter 实时工作之前你需要做这些事情。

要扫描代码问题而不先保存,请使用shift + cmd + M,您将在 vscode 终端中收到错误代码。


0
投票

EDIT2,2024 年 3 月 27 日

仅使用此配置进行实时 linting。

{
    "python.defaultInterpreterPath": "/usr/bin/python3",
    "editor.formatOnSave": true
}

编辑1 2022 年 7 月 4 日更新 新版本的 python 扩展已弃用

"python.pythonPath"
并建议改用它。

"python.defaultInterpreterPath": "/usr/bin/python3",

将这些行放入

.vscode/settings.json

{
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.pythonPath": "/usr/bin/python3",
    "editor.formatOnSave": true
}

并安装

autopep8
软件包。

使用pip安装

python3 -m pip install autopep8

或者在基于 Debian 的存储库中存在

python3-autopep8
,您可以运行

sudo apt install python3-autopep8

然后 vscode 上的 python linting 就可以工作了。

此外,警告菜单将激活。

提示我设置了 run linter onsave。

© www.soinside.com 2019 - 2024. All rights reserved.