Beginner Pygame and Python help
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
import os, pygame
File "C:\Python26\pygame.py", line 12, in
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
import pygame
File "C:\Python26\pygame.py", line 5, in
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?
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
I assume you used
pygame-1.8.1release.win32-py2.6.msi
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Yes, I did. Sorry for leaving that out.
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
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.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
import pygame
File "C:\Python26\pygame.py", line 5, in
AttributeError: 'module' object has no attribute 'display'
Thanks for the help so far.
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
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?
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
File name: program.py
Traceback (most recent call last):
File "C:\Python26\program.py", line 3, in
import pygame
File "C:\Python26\pygame.py", line 5, in
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.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
why not put in My documents or elsewhere? I count working in python folder as dangerous practice that I avoid
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392