导入 pygame 时,pylint 的导入在 VScode 中失败

Imports failing in VScode for pylint when importing pygame

导入时 pygame pylint 变得疯狂:

E1101:Module 'pygame' has no 'init' member
E1101:Module 'pygame' has no 'QUIT' member

我在网上搜索了一下,发现了这个:

"python.linting.pylintArgs": ["--ignored-modules=pygame"]

它解决了 pygame 的问题,但现在 pylint 以其他方式变得疯狂:crazy_pylint.png。 然后我找到了 "python.linting.pylintArgs": ["--ignored-files=pygame"],但它所做的是完全禁用我正在工作的整个目录的 pylint。 那么我怎么说 pygame 一切正常的 pylint?

对于E1101: 问题是大部分 Pygame 是直接用 C 实现的。现在,就性能而言,这一切都很好,但 pylint(VSCode 使用的 linter)无法扫描这些 C 文件。 不幸的是,这些相同的文件定义了一堆有用的东西,即 QUIT 和其他常量,例如 MOUSEBUTTONDOWNK_SPACE 等,以及 initquit.

要解决此问题,首先要停止忽略 pygame 模块,方法是删除 "python.linting.pylintArgs" 中的所有参数。相信我,linter 可以派上用场。

现在解决问题。对于你的常量(大写的任何东西),像这样手动导入它们:

from pygame.constants import (
    MOUSEBUTTONDOWN, QUIT, MOUSEMOTION, KEYDOWN
)

您现在可以使用它们而无需在它们前面添加 pygame.:

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
    if event.type == KEYDOWN: 
        # Code

接下来,对于您的 init 和其他函数错误,您可以通过 2 种方法手动帮助 linter 解决这些错误:

  • 在您的代码中的某处添加:# pylint: disable=no-member。这将停用整个文件的成员验证,防止显示此类错误。
  • 或者您可以将错误所在的行括起来:

    # pylint: disable=no-member
    pygame.quit()
    # pylint: enable=no-member

这与第一种方法的作用类似,但是它将效果限制在该行。

最后,对于所有其他警告,解决方案是修复。 Pylint 可以向您展示您的代码在哪些地方毫无意义,或者不符合 Python 规范。快速浏览一下您的屏幕截图,例如您的模块没有文档字符串,您已经声明了未使用的变量...... Pylint 可帮助您编写简洁、清晰和漂亮的代码。您可以忽略这些警告或隐藏它们(使用 # pylint: disable=these codes)或花一点时间清理所有内容。

在长 运行 中,这是最好的解决方案,因为它会使您的代码更具可读性,因此更易于维护,并且看起来更令人愉悦。

OP 您还可以通过自己包含 vscode 默认参数来维护您在 vscode 中找到的 pylint pygame 修复程序。 linter 快疯了 (crazy_pylint.png),因为您用自己的自定义 python.linting.pylintArgs 破坏了默认的 pylint 参数。 pygame 模块忽略修复确实有效,并且 linter 可以 return 通过在您自己的自定义 python.linting.pylintArgs.

中包含破坏的默认参数来进入非疯狂模式

来自文档:

These arguments are passed whenever the python.linting.pylintUseMinimalCheckers is set to true (the default). If you specify a value in pylintArgs or use a Pylint configuration file (see the next section), then pylintUseMinimalCheckers is implicitly set to false.

默认值 vscode 根据此传递:https://code.visualstudio.com/docs/python/linting 是:

    --disable=all,
    --enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

因此,以下是如何在 vscode 中传递所有这些默认值以及用户设置中的 --ignored-modules=pygame

    "python.linting.pylintArgs": [
        "--disable=all",
        "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
        "--ignored-modules=pygame"
    ]

根据上面@C._ 的评论,他说的绝对是实话;棉绒会有所帮助!

我肯定会在启用它的情况下编写更好的代码。

此外,我发现您可以使用启用行和逗号分隔进一步微调您的 pylinter "readable pylint messages" 此处列出:https://github.com/janjur/readable-pylint-messages/blob/master/README.md

因此,为了不忽略尾随换行符,您可以附加 enable= 列表参数以仅包含 trailing-newlines.

我真的希望这对你有帮助 OP :) 它帮助了我!

感谢提问和分享 --ignored-modules

对于特定的二进制模块,您可以将其列入 pylint 的白名单。对于 pygame 模块,它将如下所示:

{
    "python.linting.pylintArgs": [
        "--extension-pkg-whitelist=pygame"
    ]
}