3

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"]

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?

Gama11
  • 31,714
  • 9
  • 78
  • 100
Umbrelluck
  • 33
  • 1
  • 6

3 Answers3

11

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.

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
)

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

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

  • 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.

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.

underscoreC
  • 729
  • 6
  • 13
  • I tried the approach with `from pygame.constants import` but then i get pylint errors on that line. What helped for me was changing it to `from pygame.event import` – Sandro4912 Aug 26 '20 at 16:38
2

For a specific binary module you can whitelist it for pylint. For the pygame module it would be as follows:

{
    "python.linting.pylintArgs": [
        "--extension-pkg-whitelist=pygame"
    ]
}
kipkemoi
  • 21
  • 1
0

OP You can also maintain the pylint pygame fix you found in vscode by including the vscode default arguments yourself. The linter is going nuts (crazy_pylint.png) because you were clobbering the default pylint arguments with your own custom python.linting.pylintArgs. The pygame module ignore fix does work, and the linter can return to non-crazy mode by also including the clobbered default arguments in your own custom python.linting.pylintArgs.

From the docs:

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.

The defaults vscode passes according to this: https://code.visualstudio.com/docs/python/linting are:

    --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

So, here is how to pass all those defaults as well as the --ignored-modules=pygame in user settings within vscode:

    "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"
    ]

Per @C._ comment above, he's definitely speaking truth; the linter will help!

I'm writing better code with it enabled for sure.

Also, I discovered that you can further fine-tune your pylinter with the enable line and comma delimited "readable pylint messages" listed here: https://github.com/janjur/readable-pylint-messages/blob/master/README.md

So to not ignore also trailing-newlines, you would append the enable= list argument to include simply trailing-newlines.

I really hope this helps you OP :) It helped me!

Thanks for asking the question, and sharing --ignored-modules.