Errors: Module 'pygame' has no 'init' member, and others
Solution:
It's something about "pylint", not your code. The third one: Unused variable 'screen', it
is because you created the variable 'screen' but have not used it. The first one and the second one, you can solve it through configuration:
Open settings.json file add these settings:
"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",
"--unsafe-load-any-extension=y", //or add this one: --extension-pkg-whitelist=pygame
],
These settings are the default settings of pylint, you can refer to this page:
"--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",
if you only add
"--unsafe-load-any-extension=y"
or
"--extension-pkg-whitelist=pygame"
you will get "Missing module docstring" and "Missing function or method docstring" warning.
Explains:
Why did you get the first and second waring? This is because "pylint" can't lint a C extension module. You can refer to this page for more information. The "init()" function is in the file of "base.cp38-win32.pyd", under the pygame package, and "pylint" can't lint it. The "QUIT" is the same problem, it is in the "constants.cp38-win32.pyd" file.
A C extension for CPython is a shared library (e.g. a .so file on Linux, .pyd on Windows)
VS code module pygame has no init member
When I used the module "pygame
", I reproduced the problem you described:
(Although the code can be executed, there is an error "no init member")
Solution:
Please add
"python.linting.pylintArgs": [
"----extension-pkg-whitelist=1xml",
],
in "settings.json
", and use the latest version of VScode.( Version: 1.50.1 )
result:
Error: Module 'pygame' has no 'init' member
You are getting bombarded by those errors as your probably code does not conform to the PEP8 style of writing Python code.
Right-click anywhere in your file and click Format Document
or press Alt+Shift+F
to format your code automatically. After that, just look at the remaining warnings and errors and try to fix them. The errors are probably just asking you to make docstring for functions and classes.
If you don't want to write docstrings, add this to your VSCode settings.json file
"python.linting.pylintArgs": [
"--disable=C0114, C0116",
],
Run pygame codes and it shows 'Module 'pygame' has no 'init' member' and white screen window
This took me a great deal of debugging to fix, haha.
First of all, you are not escaping or updating in any of your while
loops. Once the code enters your first while
loop, nothing (the pygame.display
) ever updates after that – resulting in your white-screen syndrome.
In addition, please name your variables consistently and check for typos in your code. You created a blueSquareVy
variable only to try to refer to it as blueSquareVY
later, and you misspelled pygame.display.update()
at the end of the code. Capitalization matters – and that's just a few of the typos!
There are also logical errors within the code. You should not fill your window surface after your graphics have been drawn onto the screen. Looking at your variables, it seems that you would like the little squares to move. You would create the position variables outside of the while
loop, as if you create them within the loop, they get recreated at their initial value every iteration of the loop.
The annotated and bugfixed code:
import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
pygame.init()
windowWidth = 640
windowHeight = 480
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Shapes!')
# Renamed variables to be consistent
# Moved variables out of the while loop
greenSquareX = windowWidth / 2
greenSquareY = windowHeight / 2
blueSquareX = 0.0
blueSquareY = 0.0
blueSquareVX = 1
blueSquareVY = 1
# Joined the two while loops into one
while True:
surface.fill((200, 0, 0))
pygame.draw.rect(surface, (255, 0, 0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10))
surface.fill((0, 0, 0))
pygame.draw.rect(surface, (0, 255, 0),
(greenSquareX, greenSquareY, 10, 10))
greenSquareX += 1
greenSquareY += 1
pygame.draw.rect(surface, (0, 0, 255),
(blueSquareX, blueSquareY, 10, 10))
blueSquareX += blueSquareVX
blueSquareY += blueSquareVY
blueSquareVX += 0.1
blueSquareVY += 0.1
# Do not capitalize the .get() method for pygame.event class
for event in GAME_EVENTS.get():
if event.type == GAME_GLOBALS.QUIT:
pygame.quit()
sys.exit()
# Misspelled pygame.display
pygame.display.update()
Hope this helped!
Problems with PyGame in VS code and possible false error alerts
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"]
Related Topics
How to Check If a String Is Unicode or Ascii
Loop Through Json Data in Python
Convert Tensorflow String to Python String
Convert Numpy.Datetime64 to String Object in Python
How to Modify the Navigation Toolbar Easily in a Matplotlib Figure Window
Loading All Images Using Imread from a Given Folder
Psycopg2 Insert Python Dictionary as Json
How Can My Model Primary Key Start With a Specific Number
Is There a Short-Hand for Nth Root of X in Python
Python Searching for Partial Matches in a List
How to Convert Column With Dtype as Object to String in Pandas Dataframe
In Dictionary, Converting the Value from String to Integer
How to Compare Two Image Files Contents in Python
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Tensorflow:Attributeerror: 'Module' Object Has No Attribute 'Mul'