Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame attribute, init()

I'm trying to use Pygame with Python 3.3 on my windows 8 laptop. Pygame installed fine and when I import pygame it imports fine as well. Although when I try to execute this small code:

import pygame

pygame.init()

size=[700,500]
screen=pygame.display.set_mode(size)

I get this error:

Traceback (most recent call last):
  File "C:\Users\name\documents\python\pygame_example.py", line 3, in <module>
    pygame.init()
AttributeError: 'module' object has no attribute 'init'

I used pygame-1.9.2a0-hg_56e0eadfc267.win32-py3.3 to install Pygame. Pygame is installed in this location 'C:\PythonX' and Python 3.3 is installed in this location 'C:\Python33'. I have looked at other people having the same or similar problem and it doesn't seem to solve the error. Have I done anything wrong when installing Pygame? Or does it not support windows 8?

like image 688
user2387537 Avatar asked Nov 11 '13 17:11

user2387537


People also ask

What does pygame init () do?

pygame. init() initialize all imported pygame modules. No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.

How do I fix pygame error?

To solve the error, install the module by running the pip install pygame command. Open your terminal in your project's root directory and install the pygame module.

What does pygame quit () do?

The pygame. quit() function is sort of the opposite of the pygame. init() function: it runs code that deactivates the Pygame library. Your programs should always call pygame.

What is fill in pygame?

fill function which fills the Surface object, our screen, with the colour red. This basically makes everything we have drawn on the screen Surface become visible and updates the contents of the entire display. Without this line, the user wouldn't see anything on their pygame screen.


Video Answer


2 Answers

After import pygame pygame.init()

I got this error message and programm didnt work: " AttributeError: module 'pygame' has no attribute 'init' "

because i named the file "pygame.py"...

When i chaned filename to "pygametest.py" everything worked.

Naming the filename exactly like the modulename seems to confuse python...

like image 52
Giovanni Stallione Avatar answered Oct 04 '22 17:10

Giovanni Stallione


You have a directory named pygame in your path somewhere.

$ mkdir pygame  # empty directory
$ python3.3
>>> import pygame
>>> pygame
<module 'pygame' (namespace)>
>>> pygame.__path__
_NamespacePath(['./pygame'])

Remove or rename this directory, it is masking the actual pygame package.

If you use print(pygame.__path__) it'll tell you where the directory was found; in the above example it was found relative to the current directory (./).

like image 27
Martijn Pieters Avatar answered Oct 04 '22 17:10

Martijn Pieters