I can't get my python programs with pygame to work.
I'm a complete noob, but I have gotten some very simple python programs I wrote to run on IDLE3. I copied the Bat and Ball program from MagPi issue 1 and when I try to run it, I get the following error:
> Traceback (most recent call last):
> File "/home/pi/MyFiles/BallAndBat.py", line 3, in <module>
> import pygame
> ImportError: No module named pygame
So I went to elinux.org and followed the instructions but got another message (this time in the Terminal):
> In file included from src/imageext.c:47:0:
> src/pygame.h:80:20: fatal error: Python.h: No such file or directory
> compilation terminated.
> error: command 'gcc' failed with exit status 1
I was hoping someone could tell me what I'm doing wrong. BTW I'm running raspbian wheezy I downloaded off of this website. Oh, and I'm sorry if this has already been addressed somewhere. I did look and didn't find it anywhere, but that doesn't mean much cause my internet is really slow on my RPi so I may have given up a little too soon.
Re: pygame module missing
Sounds like pygame is either not installed, or installed but not available for Python 3.x (IDLE 3), or both.
From a terminal window:
Just tried on my RPi running Raspbian wheezy and it still shows 'No module named pygame' under IDLE 3, but loads OK on IDLE 2 (Python 2.x). Looks like more work is required to install pygame for python 3.x ... trying it now.
From a terminal window:
Code: Select all
sudo apt-get install python-pygame
Re: pygame module missing
Seems the installation of the latest pygame for python 3 has already been covered in this topic:
http://www.raspberrypi.org/phpBB3/viewt ... 32&t=13032
So this worked for me:
# get pygame source code
# install dependencies
(that's all one line)
# build and install pygame
Whew. After all that, import pygame works in IDLE 3. 
Or you could just stick with pygame tutorials meant for python 2.x and use IDLE 2 and the easily installed pygame package.
http://www.raspberrypi.org/phpBB3/viewt ... 32&t=13032
So this worked for me:
# get pygame source code
Code: Select all
sudo apt-get install mercurial
Code: Select all
hg clone https://bitbucket.org/pygame/pygame
Code: Select all
cd pygame
Code: Select all
sudo apt-get install python3-dev python3-numpy libsdl-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev
# build and install pygame
Code: Select all
python3 setup.py build
Code: Select all
sudo python3 setup.py install

Or you could just stick with pygame tutorials meant for python 2.x and use IDLE 2 and the easily installed pygame package.
Re: pygame module missing
First off, I really appreciate the help.
Unfortunately, that solution doesn't seem to work for me. That's the solution I tried from elinux where I got that second error message. Also, the get-apt install python-pygame didn't work either.
I'll try that solution (the elinux one) again and see if it works this time.
BTW - the bit about python 2...is that why the Python Games program works? I just ran four in a row (until it crashed on me), which shouldn't work without pygame. Am I right in thinking that aren't I?
Unfortunately, that solution doesn't seem to work for me. That's the solution I tried from elinux where I got that second error message. Also, the get-apt install python-pygame didn't work either.

I'll try that solution (the elinux one) again and see if it works this time.
BTW - the bit about python 2...is that why the Python Games program works? I just ran four in a row (until it crashed on me), which shouldn't work without pygame. Am I right in thinking that aren't I?
Re: pygame module missing
Did you actually try it as I've posted it? I added python3-dev and python3-numpy to the list of packages installed on the elinux.org site, so if you didn't install those that's probably why you're getting that error.acacia460 wrote:Unfortunately, that solution doesn't seem to work for me. That's the solution I tried from elinux where I got that second error message. Also, the get-apt install python-pygame didn't work either.
I'll try that solution (the elinux one) again and see if it works this time.
Re: pygame module missing
GADZOOKS IT WORKS!!
You are right, I noticed the differences after I had already posted. Now I'm getting a completely different error message. I'm sure it's just a mistake in my code though...
Either way, Thanks!!!!!
You are right, I noticed the differences after I had already posted. Now I'm getting a completely different error message. I'm sure it's just a mistake in my code though...
Either way, Thanks!!!!!
Re: pygame module missing
CONFIRMATION!!!
Fixed my code...the program works!
Thanks again!!
Fixed my code...the program works!
Thanks again!!
Re: pygame module missing
I got it to work by using
After that I was able to use import pygame in Python3
Code: Select all
sudo apt-get install python3-pygame