Install Pygame for Python 3.1.2 on Ubuntu

I am reading the book Introduction to Computer Science using Python and Pygame by Paul Craven (note: legally available for free online). In the book, he uses a combination of Python 3.1.3 and Pygame 1.9.1. In my Linux Ubuntu machine, I have Python 3.1.2, but even after I installed sudo apt-get python-pygame (version 1.9.1), Python 3.1.2 cannot import pygame.

Python 3.1.2 (r312:79147, Sep 27 2010, 09:45:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pygame Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pygame 

Python 2.6.5 imports it without fuss, however

 Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pygame >>> 

Do you know about any problems for Linux / Ubuntu Python 3.1.2 (Prof. Craven used Windows in his book)? Why did Pygame 1.9.1 work for Python 3.1.3, but not for 3.1.2?

Thanks for any pointers. (-)

+6
source share
6 answers

This is because installing the python-pygame package installs it for the default version of Python on your system, 2.6.5 in this case. You must download the pygame package and use setup.py to install it in 3.1.2.

-1
source

PyGame in Python 3 remains experimental, but these steps worked for me on Ubuntu 11.10:

 sudo apt-get install mercurial python3-dev libjpeg-dev libpng12-dev libportmidi-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev libx11-dev ttf-freefont libavformat-dev libswscale-dev hg clone -u 01b2cb16dc17 https://bitbucket.org/pygame/pygame cd pygame python3 config.py 2to3 setup.py -w python3 setup.py build sudo python3 setup.py install 

(You can remove -u 01b2cb16dc17 to try the latest version, 01b2cb16dc17 worked for me.)

+9
source

I don't like opening the old post again, but it was harder for me to install pygame with a version of python that was not the default build of Ubuntu. So I created this tutorial / how to:

Install python3.1 and pygame1.9.1 on Ubuntu

I hope this helps the next unsuccessful soul try it.

+3
source

I followed the @ Søren method, but without the -u number.

The only complication was a few compilation errors on the last line, all due to differences in syntax and Unicode between Python 2 and Python 3, but with a little checking of the web documentation it was a few minutes with the text editor changing the following files (all paths belong to the directory pygame created at boot time):

 gedit build/lib.linux-x86_64-3.2/pygame/colordict.py gedit build/lib.linux-x86_64-3.2/pygame/tests/test-utils/png.py gedit build/lib.linux-x86_64-3.2/pygame/examples/movieplayer.py 

Line numbers from compiler error messages are great for giving you a place to start. Beware:

 1 remove all references to u"xxxx" colours 2 use Python3 syntax for exceptions 3 change all print commands to Python3 equivalents 

Then reinstall the final compilation command:

 sudo python3 setup.py install 

If you miss one or two or make a mistake, just keep working on editing the loop and recompiling it until it works.

By the way, I specifically did not give details about the compiler messages, because I expect that they will depend on the current assembly that you load. The files I needed to change were for the version "1.9.2pre" downloaded from the date of this post.

0
source

The python-pygame package compiles only for python2.6 and python2.7 where I am. You will need to install it again, possibly from the python3 source branch.

-1
source
-1
source

Source: https://habr.com/ru/post/891756/


All Articles