amay20
amay20

Reputation: 147

Pygame error: 'pygame' has no attribute 'init'

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

Upvotes: 7

Views: 35211

Answers (9)

user19345539
user19345539

Reputation:

try:

python -m pip uninstall pygame
python -m pip install pygame

This should reinstall pygame.

Upvotes: 0

Mohamed Moawia
Mohamed Moawia

Reputation: 566

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

Upvotes: 23

Homan
Homan

Reputation: 1

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) or the python's interpreter.

you can also seek info at www.pygame.org

Upvotes: 0

Rajendro Sau
Rajendro Sau

Reputation: 71

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

Upvotes: 0

Gray Hat
Gray Hat

Reputation: 47

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

Upvotes: 0

Ayaan
Ayaan

Reputation: 167

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

Upvotes: 0

srattigan
srattigan

Reputation: 664

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!

Upvotes: 0

René Dudfield
René Dudfield

Reputation: 611

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

Upvotes: 1

SingleNegationElimination
SingleNegationElimination

Reputation: 156278

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.

Upvotes: 4

Related Questions