pepijntje02
Posts: 18
Joined: Mon Jan 04, 2016 6:55 pm

Python pygame in python 2.7.9 and 3.4.2

Mon Jan 04, 2016 7:09 pm

Hello,

This is my first time posting on this forum. I am new with the raspberry pi, but I have some experience with programming in python and with linux.

I want to try the pygame libary in python. I have installed pygame in both, python 2.7 and 3.4. When I execute the following line in de IDLE then there are no errors:

Code: Select all

import pygame
from pygame.locals import *
When I choose to try a python script that I have found on the website it does not work annymore. https://www.raspberrypi.org/learning/as ... oystick.md

Code: Select all

import pygame

from pygame.locals import *
from sense_hat import SenseHat

pygame.init()
pygame.display.set_mode((640, 480))
sense = SenseHat()
sense.clear()

running = True

while running:
    for event in pygame.event.get():
        print(event)
        if event.type == QUIT:
            running = False
            print("BYE")
I have tried the same scirpt in both python 2.7 and 3.4 but both give an error back. I also tried with the next line on top of the code: #! /usr/bin/python and #! /usr/bin/python2.7. Those lines should tell the script from where to execute the code if I am right.

Does anybody know what I am doing wrong in this code? I do not know it annymore. If I have found the solution in the mean time I will tell it here.

thanks for your time,

Pepijntje02

Navyscourge
Posts: 258
Joined: Sat Oct 24, 2015 1:50 pm

Re: Python pygame in python 2.7.9 and 3.4.2

Mon Jan 04, 2016 8:18 pm

With a first line of #! /usr/bin/python, a script when run in a bash shell will execute the script using the program /usr/bin/python. You are running the script from the IDLE editor, so these should not be necessary.

It would be useful if you could quote the exact text of the error message so we can see what went wrong

User avatar
DougieLawson
Posts: 36833
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python pygame in python 2.7.9 and 3.4.2

Mon Jan 04, 2016 8:22 pm

No matter how I'm going to run a python program I leave a shebang (#!/usr/bin/pythonX) line in there to remind me which flavour it's written for. There are enough syntax differences that trying to a run an un-modified python2 program with python3 is a massive frustration.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

pepijntje02
Posts: 18
Joined: Mon Jan 04, 2016 6:55 pm

Re: Python pygame in python 2.7.9 and 3.4.2

Mon Jan 04, 2016 8:29 pm

Hello,

Both thanks for the fast response! Of course I should post the error I get in IDLE. I should have done that in the first post, sorry for that!

The error I receive is as follow:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/pygame.py", line 2, in <module>
    import pygame
  File "/home/pi/Documents/pygame.py", line 4, in <module>
    from pygame.locals import *
ImportError: No module named locals
That is in Python 2.7 with the following code:

Code: Select all

#!/usr/bin/python2.7
import pygame

from pygame.locals import *
from sense_hat import SenseHat

pygame.init()
pygame.display.set_mode((640,480))
sense=SenseHat()
sense.clear()

running = True

while running:
    for event in pygame.event.get():
        print(event)
        if event.type == QUIT:
            running = False
            print("BYE")
I hope that some of you know something with this error!

kind regards

User avatar
DougieLawson
Posts: 36833
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python pygame in python 2.7.9 and 3.4.2

Mon Jan 04, 2016 9:12 pm

The SenseHAT stuff is new code so will have been written in python3.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

pepijntje02
Posts: 18
Joined: Mon Jan 04, 2016 6:55 pm

Re: Python pygame in python 2.7.9 and 3.4.2

Tue Jan 05, 2016 7:10 am

allright, but the error occurs also in python 3.4, and it is an error about the pygame module, not about the SenseHat module. So I understand that it is possible because the SenseHat is new that the code is written for python 3. What I don't understand is why the error tells nothing about the SenseHat but about the pygame module.
In the evening I will be able to try a pygame script without the SenseHat. So I will do that and look if that gives an error.

Thanks for your respons!

User avatar
DougieLawson
Posts: 36833
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Python pygame in python 2.7.9 and 3.4.2

Tue Jan 05, 2016 9:41 am

sudo apt-get install --reinstall python{,3}-pygame
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

pepijntje02
Posts: 18
Joined: Mon Jan 04, 2016 6:55 pm

Re: Python pygame in python 2.7.9 and 3.4.2

Thu Jan 07, 2016 12:17 pm

Hello,

When I try to only import pygame, or pygame with the snesehat code then there are no problems. So the problems are in the from pytgame.locals import * and in the pygame.init(). I have to discover more about pygame by myself I think, because it looks like those errors are just beginners faults.

Thank for your answers! They were sitll very helpfull and I am happy to see that on the forum are people helpful, even if the questions are "stupid" or simple! ;)

jehutting
Posts: 140
Joined: Sun Feb 15, 2015 8:37 am
Location: The Netherlands

Re: Python pygame in python 2.7.9 and 3.4.2

Thu Jan 07, 2016 6:51 pm

Your script "/home/pi/Documents/pygame.py" shouldn't be named pygame.py.
See here why and how to solve it.

pepijntje02
Posts: 18
Joined: Mon Jan 04, 2016 6:55 pm

Re: Python pygame in python 2.7.9 and 3.4.2

Thu Jan 07, 2016 7:35 pm

Hello jehutting,

Thanks for your reaction! This solved the problem and now does the script work! Thanks a lot, this saved a lot of searching time for me! ;)

Thanks, now the topic is answerd, should I put that in the title or something else so everybody sees that this question is solved (like on the arch forum)?

Thanks a lot!

dgordon42
Posts: 773
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Python pygame in python 2.7.9 and 3.4.2

Thu Jan 07, 2016 7:49 pm

Hi,
First off, Dougie is right about Python3. The code you are trying from the website is written in Python 3, and may produce errors or unexpected behavior when run in Python2. Stick to Python3 and/or IDLE3 for the Astro Pi Guides.
The shebang tip is also a good one, I use it on all my Python scripts, even the ones that run on Windows computers. The very first line on a Python2 script should be

Code: Select all

#!/usr/bin/python
and Python3 scripts should start with

Code: Select all

#!/usr/bin/python3
The error you reported does indeed seem to be connected to pygame.locals. The pygame.locals file is located at:

Code: Select all

/usr/lib/python3/dist-packages/pygame/locals.py
in Python3. Do you have this file on your machine?
Pygame comes as standard in Raspbian Jessie. Did you install it from somewhere else?

Which version of Pygame do you have? You can check it in a terminal like this:-

Code: Select all

flash@RaspberryPlus ~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.ver
'1.9.2a0'
>>> quit()
flash@RaspberryPlus ~ $
Dave.

rubikwizard
Posts: 95
Joined: Tue Jan 10, 2012 10:37 pm
Location: West Yorkshire
Contact: Website

Re: Python pygame in python 2.7.9 and 3.4.2

Thu Jan 07, 2016 9:42 pm

jehutting wrote:Your script "/home/pi/Documents/pygame.py" shouldn't be named pygame.py.
See here why and how to solve it.
I am a teacher and have had many frustrating lessons with this very issue. E.g. students using something like import random and then naming their script random.py. I've got quite used to spotting it now!

Return to “Beginners”