0

I'm trying to learn pygame and am having trouble getting it to work. (I'm using python 3.8.5 64 bit) First I installed pygame with python3 -m pip install -U pygame --user and that worked fine and I could type import pygame with no errors. The next line, pygame.init(), gave me an error though. The error was Module 'pygame' has no 'init' memberpylint(no-member). I went to this link for a solution Why does it say that module pygame has no init member? And found one which was to paste "python.linting.pylintArgs": ["--extension-pkg-whitelist=lxml"] into .vscode>settings.json but this didn't make the errors go away (yes I saved both files). The real confusing part to me though is that the code works how I want it to (to make a new, blank window for the game and close with the red x in the top right) even with these errors. I think every time I call a method from pygame I get an error even if the methods working and I just want to make sure leaving it how it is won't break things in the future.

print(pygame) gets me

<module 'pygame' from 'C:\\Users\\jgood\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python38\\site-packages\\pygame\\__init__.py'>

dir(pygame)gets me

pygame 1.9.6

Finally here is the code

#where most python goes

import pygame

#initating our pygame stuff won't work without this line
pygame.init() <--- Module 'pygame' has no 'init' memberpylint(no-member)

#create screen like html document.getelementbyid.ect.ect
screen = pygame.display.set_mode((800, 600)) # this takes two args height and width in pixels

running = True # flag

#game loop
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: <----- Module 'pygame' has no 'QUIT' memberpylint(no-member)
            running = False

and here is my settings.json file

{
    "python.pythonPath": "C:\\Users\\jgood\\AppData\\Local\\Microsoft\\WindowsApps\\python3.8.exe",
    "python.linting.pylintArgs": ["--extension-pkg-whitelist=lxml"]
}
Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21

1 Answers1

0

So there's a difference between the python interpreter throwing an Exception when you run the code (commonly referred to as an "error") and the warnings that pylint is showing you.

pylint's job is to try to warn you about potential issues prior to running your code. It's a static analysis library that tries to infer potential problems. One thing it sometimes has trouble with is detecting "members" for complex or dynamic packages. Pygame is such a package.

The warnings its giving you here are telling you that pylint thinks there's no init() function in pygame. There is, it's just that pylint can't see it with its code analysis. You can handle this in several ways:

  • you can fully disable pylint for pygame
  • you can disable pylint on a per-line basis
  • you can disable pylint on a per-block basis
  • you can disable pylint on a per-line/per-block basis specific to a particular warning

You can read about the various options for suppressing pylint warnings here

For now I suggest you use the information in the post you linked to -- but notice there's a typo in that author's answer that is corrected in the comments. You'll want to change ["--extension-pkg-whitelist=lxml"] to read ["--extension-pkg-whitelist=pygame"]

kerasbaz
  • 1,774
  • 1
  • 6
  • 15