9
votes

Here is my small program,

import pygame
pygame.init()

Here is my compilation command.

python myprogram.py

Compilation error,

  File "game.py", line 1, in 
    import pygame
  File "/home/ubuntu/Documents/pygame.py", line 2, in 
    pygame.init()
AttributeError: 'module' object has no attribute 'init'

I have pygame installed in my ubuntu, It is installed in

/usr/lib/python2.6/dist-packages/pygame

I found tht from IDLE, If I execute both of this statements, It works fine.

3
Where is "game.py" saved? If pygame is in site-packages, but there is also a "pygame.py" in the same folder as "game.py", then the file in the same folder w/ the same name will be imported instead. - Adam Vandenberg
Note that this is not a compiletime error. Importing, like nearly everything else (except parsing) happens at runtime in Python. And python somefile.py of course runs the file. - user395760

3 Answers

15
votes

Delete the "pygame.py" file in your Documents folder, it is shadowing the real pygame you've installed.

It looks like you first saved your small test program as "pygame.py", then renamed it to "game.py".

0
votes
  1. Here is my compilation command:python myprogram.py but you have error in File "game.py", line 1, in oO

  2. I was have same problem and solve it by renaming of main .py file. He can't be the same as module i.e pygame

-2
votes

Rename pygame.py to another_name_as_pygame.py and this is your new Code:

import pygame
from pygame.locals import *
pygame.init()