在 Visual Studio Code 中检查 Python 代码

Linting 可以突出显示 Python 源代码中的语义和风格问题,这通常有助于您识别和纠正细微的编程错误或可能导致错误的编码实践。例如,linting 可以检测到未定义变量的使用、对未定义函数的调用、缺少括号,甚至更微妙的问题,如尝试重新定义内置类型或函数。Linting 与 格式化 不同,因为 linting 分析代码的运行方式并检测错误,而格式化仅重构代码的外观。

注意:Python扩展的语言服务器默认启用了语法错误检测。要了解如何配置语言服务器,请参阅语言服务器设置。本文档介绍了如何为额外的代码检测启用linting,包括风格检查。

选择一个linter

VS Code Marketplace中搜索您选择的linter扩展。如果您愿意,可以同时使用多个linter。

微软发布了以下用于Python的linting扩展:

Linter Extension
Pylint https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
flake8 https://marketplace.visualstudio.com/items?itemName=ms-python.flake8
mypy https://marketplace.visualstudio.com/items?itemName=ms-python.mypy-type-checker

社区提供的Linting扩展:

Linter Extension
Ruff https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff
mypy https://marketplace.visualstudio.com/items?itemName=matangover.mypy

注意: 如果您在上面的表格或市场中找不到您喜欢的linter,您可以通过扩展来添加支持。您可以使用Python扩展模板将新的Python工具集成到VS Code中。

通用设置

您可以参考每个linter扩展的README以获取有关支持的设置的更多详细信息。大多数linter扩展支持以下设置:

Setting Default Description
args [] Arguments to be passed to the linter. Note: The officially supported linters run on individual open files. Make sure your configuration applies in that scenario.
importStrategy useBundled When set to useBundled, the extension uses the version of the tool that it ships with. When set to fromEnvironment, it attempts to load from your selected Python environment first, otherwise it falls back to the bundled version.
path "" Path to the linter binary to be used for linting. Note: Using this option may slow down formatting.
interpreter [] When set to a path to a Python executable, the extension will use that to launch the linting server and its subprocesses.
showNotifications off Controls when notifications are displayed by the extension. Supported values are off, always, onError, and onWarning.

禁用代码检查

如果安装了Linters,默认情况下它们是启用的。你可以通过禁用扩展来禁用它们,每个工作区都可以单独设置。

运行代码检查

当打开或保存Python文件时,Linting将自动运行。

错误和警告显示在问题面板中(⇧⌘M(Windows, Linux Ctrl+Shift+M))用于打开的文件,并且也在代码编辑器中高亮显示。将鼠标悬停在带下划线的问题上会显示详细信息:

编辑器中的Linting消息和问题面板

代码操作

一些linter可能会提供代码操作,这些操作可以帮助解决报告的问题。您可以参考您首选的linter扩展下的功能贡献部分,以了解它提供了哪些代码操作。

日志记录

当您从下拉菜单中选择时,linter的日志可以在输出面板中查看(⇧⌘U (Windows Ctrl+Shift+U, Linux Ctrl+K Ctrl+H))。

您可以通过从命令面板运行开发者:设置日志级别命令来更改linter扩展的日志级别(⇧⌘P (Windows, Linux Ctrl+Shift+P))。从扩展日志组中选择扩展,然后选择所需的日志级别。

严重性

Linters 报告问题时带有一些预定义的严重性。这可以通过使用 linter 的 severity 设置来更改。有关支持的数值和严重性级别的更多详细信息,请参阅每个 linter 扩展的 README。

排查代码检查问题

Issue Cause Solution
Linter extension is not reporting any problems. No Python has been selected for your workspace. Look at the logs for the linter you are using and check the path to the Python environment it's using. If there is no Python selected, run the Python: Select Interpreter command from the Command Palette and select an existing interpreter for your workspace.
The "You have deprecated linting or formatting settings" notification is displayed If you are seeing this notification, it means you have settings such as python.linting or python.formatting in VS Code. These settings are no longer supported by the Python extension, as linting and formatting support has been migrated to tools extensions. Find where these settings are defined in VS Code by opening the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and running the Preferences: Open User Settings (JSON) command. If they're not in your User settings, then run the Preferences: Open Workspace Settings (JSON) command. Then delete the deprecated settings.
Note: If you're using any of the extension in the Remote Development extension pack, you can also check the remote settings by running the Preferences: Open Remote Settings (JSON) command.
Linting doesn't work even though I have a linter extension installed. Linting can fail for different reasons, such as an unsupported version of Python being used, or the linter isn't configured correctly. Check the linter extension's Output channel to understand why the linter has failed (run the Output: Focus on Output command in the Command Palette, and then select the linter extension channel).

下一步

  • Formatting - 了解如何格式化你的Python代码。
  • 调试 - 学习如何在本地和远程调试Python。
  • Testing - 配置测试环境并发现、运行和调试测试。
  • Basic Editing - 了解强大的VS Code编辑器。
  • Python Extension Template - 创建一个扩展,将您喜欢的linter集成到VS Code中。