导入 pygame 时,在 VScode for pylint 中导入失败 [英] Imports failing in VScode for pylint when importing pygame

查看:102
本文介绍了导入 pygame 时,在 VScode for pylint 中导入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入 pygame 时 pylint 会发疯:

When importing pygame pylint is going crazy:

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

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

I have searched the net and I have found this:

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

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

It solves the problem with pygame, but now pylint is going crazy in other way: crazy_pylint.png. Then I have found "python.linting.pylintArgs": ["--ignored-files=pygame"], but what it does is completely disabling pylint for the whole directory I am working in. So how do I say pylint that everything is OK with pygame?

推荐答案

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

For E1101: The problem is that most of Pygame is implemented in C directly. Now, this is all well and dandy in terms of performance, however, pylint (the linter used by VSCode) is unable to scan these C files. Unfortunately, these same files define a bunch of useful things, namely QUIT and other constants, such as MOUSEBUTTONDOWN, K_SPACE, etc, as well as functions like init or quit.

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

To fix this, first things first, stop ignoring the pygame module by removing all your arguments in "python.linting.pylintArgs". Trust me, the linter can come in handy.

现在解决问题.对于您的常量(大写字母),请像这样手动导入它们:

Now to fix the problems. For your constants (anything in caps), manually import them like so:

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

您现在可以使用这些,而无需在 pygame. 之前添加它们:

You can now use these without prepending them with pygame.:

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

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

Next, for your init and other functions errors, you can manually help the linter in resolving these, by way of 2 methods:

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

  • Either add this somewhere in your code: # pylint: disable=no-member. This will deactivate member validation for the entire file, preventing such errors from being shown.
  • Or you can encase the line with the error:

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

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

This is similar to what the first method does, however it limits the effect to only that line.

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

Finally, for all your other warnings, the solution is to fix them. Pylint is there to show you places in which your code is either pointless, or nonconforming to the Python specs. A quick glance at your screenshot shows for example that your module doesn't have a docstring, that you have declared unused variables... Pylint is here to aid you in writing concise, clear, and beautiful code. You can ignore these warnings or hide them (with # pylint: disable= and these codes) or spend a little time cleaning up everything.

从长远来看,这是最好的解决方案,因为它会使您的代码更具可读性和可维护性,而且看起来更令人愉悦.

In the long run, this is the best solution, as it'll make your code more readable and therefore maintainable, and just more pleasing to look at.

这篇关于导入 pygame 时,在 VScode for pylint 中导入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆