2
votes

I just downloaded Pygame but is running into an error: AttributeError: module 'pygame' has no attribute 'init'. I was looking around on stackoverflow and someone fixed the error by renaming the files (which I don't know how to do). The directory is the following

Python -> Lib -> site-packages ->pygame

The confusing part for me is inside the pygame folder

pygame > Include, Lib
   Include > pygame > (many hh files)
   Lib > SitePackages > pygame > init (I know that it has the attribute init)

Since there is '3' pygame folder, is this causing the error for my program?

import pygame, sys
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
9

9 Answers

8
votes

Just rename your file from pygame.py to myTestGame.py for example because you're importing the same file when you use import pygame

3
votes

It looks as though you have "downloaded" a source distribution of pygame, and then copied the content into site-packages. that works fine for most pure-python libraries, but not for ones with c-extensions, like pygame. You are probably best served by using an installer, as pygame is a bit tedious to build from source.

Visit http://www.pygame.org/download.shtml and choose an installer for your OS. note that the first two links are source downloads, and not what you need.

1
votes

The most current, best way to install pygame is available at: https://www.pygame.org/wiki/GettingStarted

Currently, for windows, it says to install it like this in a cmd window: py -m pip install pygame --user

0
votes

Many windows users have difficulty installing Python, getting the path vars right and then adding in additional modules. A convenient solution I find for many is to use a portable version they can put on USB (Or Desktop) and run anywhere, easily.

Download and install this: Portable Python Pack

Install to USB or Desktop

From the installed folder, open PyScripter (or another if you prefer)

Open and run your PyGame or other Python code.

NOTE: This is Python 2.7, but it has PyGame and a range of other modules packed in- check it out before download and try if suited to your needs!

0
votes

You have to install pygame. If you did please try it with pip. In terminal or command line type pip install pygame and if that does not work do python -m pip install pygame and if that does not work do py -m pip install pygame

0
votes

it's a common mistake which is not still fixed by now if you are running your code on a IDE like , "VSC" or "pycharm" and such a kind. you will get this error.

MAKE SURE that you have installed pygame correctly using pip and try to run the code using Git Bash (by typing "python "filename.py"" without quotations ) or the python's interpreter.

you can also seek info at www.pygame.org

0
votes

i recommend y'all to use this code

import pygame, sys
from pygame.locals import *

screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Pygame Title")

def close():
    sys.exit()
    pygame.quit()

while True:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
               close()
0
votes

1 first uninstall your IDE

2 reinstall IDE in another folder

3 goto IDE terminal and install Pygame using pip version 19.0.3

4 Then your problem is solved

0
votes

as Mohamed Moawia said we need to rename the file because we're importing the same file when we use import and it seems to generate a conflict, when i renamed the file, problem solved, i added a picture here to show the error and you can see different name files.enter image description here