Imports failing in VScode for pylint when importing pygame

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.

Leave a Comment