acacia460
Posts: 6
Joined: Wed Oct 03, 2012 3:07 am

pygame module missing

Fri Oct 05, 2012 6:04 pm

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.

User avatar
billb
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm

Re: pygame module missing

Fri Oct 05, 2012 6:59 pm

Sounds like pygame is either not installed, or installed but not available for Python 3.x (IDLE 3), or both.

From a terminal window:

Code: Select all

sudo apt-get install python-pygame
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.

User avatar
billb
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm

Re: pygame module missing

Fri Oct 05, 2012 7:44 pm

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

Code: Select all

sudo apt-get install mercurial

Code: Select all

hg clone https://bitbucket.org/pygame/pygame

Code: Select all

cd pygame
# install dependencies

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
(that's all one line)

# build and install pygame

Code: Select all

python3 setup.py build

Code: Select all

sudo python3 setup.py install
Whew. After all that, import pygame works in IDLE 3. :mrgreen:

Or you could just stick with pygame tutorials meant for python 2.x and use IDLE 2 and the easily installed pygame package.

acacia460
Posts: 6
Joined: Wed Oct 03, 2012 3:07 am

Re: pygame module missing

Fri Oct 05, 2012 8:16 pm

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

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?

User avatar
billb
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm

Re: pygame module missing

Fri Oct 05, 2012 8:31 pm

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.
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
Posts: 6
Joined: Wed Oct 03, 2012 3:07 am

Re: pygame module missing

Fri Oct 05, 2012 9:02 pm

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

acacia460
Posts: 6
Joined: Wed Oct 03, 2012 3:07 am

Re: pygame module missing

Fri Oct 05, 2012 9:05 pm

CONFIRMATION!!!

Fixed my code...the program works!

Thanks again!!

trsmith50
Posts: 2
Joined: Tue Mar 11, 2014 9:22 pm

Re: pygame module missing

Mon Jul 03, 2017 8:38 pm

I got it to work by using

Code: Select all

sudo apt-get install python3-pygame
After that I was able to use import pygame in Python3

Return to “Python”