6

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2387537
  • 361
  • 3
  • 9
  • 17
  • 2
    What does `import pygame; print(pygame.__file__)` print? – Martijn Pieters Nov 11 '13 at 18:00
  • Traceback (most recent call last): File "", line 1, in print(pygame.__file__) AttributeError: 'module' object has no attribute '__file__' @MartijnPieters – user2387537 Nov 11 '13 at 18:03
  • Interesting. No idea what you imported there, but I am trying to figure out if that is actually the `pygame` module or something else. It is certainly possible that the 'real' `pygame` module has no `.__file__` attribute, but it *is* surprising. Does `print(pygame)` give any detail? – Martijn Pieters Nov 11 '13 at 18:07
  • Also, I want you to add the `print()` statements to your script, not run them in a Python shell please. – Martijn Pieters Nov 11 '13 at 18:07
  • 1
    When I printed `print(pygame.__file__)` in my script I still got the same error as before and when I printed `print(game)` in my script I got this: ``. @MartijnPieters – user2387537 Nov 11 '13 at 18:12

4 Answers4

14

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...

8

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Nice detective work. Isn't this supposed to happen only with directories containing `__init__.py`? – interjay Nov 11 '13 at 18:20
  • 1
    @interjay: No, not in Python 3, which added namespace support (what `setuptools` does with special `__init__.py` files). – Martijn Pieters Nov 11 '13 at 18:22
  • @interjay: see http://docs.python.org/3/whatsnew/3.3.html#pep-420-implicit-namespace-packages – Martijn Pieters Nov 11 '13 at 18:24
  • 1
    I see. I guess the [documentation](http://docs.python.org/3.3/tutorial/modules.html) is outdated then because it says: "The `__init__.py` files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as `string`, from unintentionally hiding valid modules that occur later on the module search path." – interjay Nov 11 '13 at 18:25
  • 1
    @interjay: these are not packages. These are namespaces. :-P – Martijn Pieters Nov 11 '13 at 18:25
  • 1
    @interjay: but if you feel that the tutorial is now no longer correct, do feel free to file a bug with the Python project. – Martijn Pieters Nov 11 '13 at 18:28
  • 1
    btw: You can get the same error if you put your code in file `pygame.py` – furas Nov 11 '13 at 19:28
  • @furas: but then `import pygame; print(pygame.__file__)` prints a filename for that module or package. – Martijn Pieters Nov 11 '13 at 19:29
1

I also named my app pygame.py and i had the same issue but when i changed it, it worked for me .

GodMode JSON
  • 11
  • 1
  • 2
0

Try to place the pygame module in .\PythonX\Lib\site-packages\

I use pip to install all modules I need.

Have you downloaded pygame and manually placed it in the PythonX folder ? Maybe the error comes from that.

D_00
  • 1,440
  • 2
  • 13
  • 32