Ok, I just learned the basics of Python yesterday, and because of other programming knowledge, I picked it up pretty fast. So then I decided to try out pygame, and I installed it directly to my Python26 directory. But none of the example code I paste from any website doesn't work. At first I tried the code from here: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html

File name: pygame.py
Traceback (most recent call last):
File "C:\Python26\pygame.py", line 11, in <module>
import os, pygame
File "C:\Python26\pygame.py", line 12, in <module>
from pygame.locals import *
ImportError: No module named locals

And even if I tried commenting something like that out, another error about a module not being found would come up. So then I tried a simple program. The site said it basically does nothing, so I wanted to see if it would compile. This is the code:

#! /usr/bin/env python

import pygame
from pygame.locals import *
screen = pygame.display.set_mode((640, 400))

while 1:
     pass

I got an error there saying

File name: pygame.py
Traceback (most recent call last):
File "C:\Python26\pygame.py", line 3, in <module>
import pygame
File "C:\Python26\pygame.py", line 5, in <module>
screen = pygame.display.set_mode((640, 400))
AttributeError: 'module' object has no attribute 'display'

So I really do not know what is wrong, since I'm pretty sure I installed it right and I'm just copying example code. Any ideas?

I assume you used
pygame-1.8.1release.win32-py2.6.msi

Yes, I did. Sorry for leaving that out.

Please do not call your program the same name as a module, Python will load the program first at the import statement! In this case pygame.py, so delete that program name from your work directory and name it something else like my_pygame1.py.

This goes for other import modules like random, beginners often call their random test program 'random.py', that will flop!

Thanks for postig the whole error traceback, I couldn't have figured it out without.

I changed the name to program.py and I tried both the example game and the other smaller script, and both had a message like this:

File name: program.py
Traceback (most recent call last):
File "C:\Python26\program.py", line 3, in <module>
import pygame
File "C:\Python26\pygame.py", line 5, in <module>
AttributeError: 'module' object has no attribute 'display'

Thanks for the help so far.

Do you think there was a problem with the pygame installation? Is there a way to check if the "missing module attributes" in the errors statements can be found in the right files?

File name: program.py
Traceback (most recent call last):
File "C:\Python26\program.py", line 3, in <module>
import pygame
File "C:\Python26\pygame.py", line 5, in <module>
AttributeError: 'module' object has no attribute 'display'

You still have a program left in C:\Python26\ that is named pygame.py. You created that file name. Please rename or erase it!!!

pygame is a package normally located at
C:\Python26\Lib\site-packages\pygame

I would recommend that you create a folder
C:\Python26\Atest where you can save all your test files.

why not put in My documents or elsewhere? I count working in python folder as dangerous practice that I avoid

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.