Installing Pygame for Python 3.1.2 in Ubuntu
I'm 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 sudo apt-get installed python-pygame (version 1.9.1), Python 3.1.2 can't 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>
Imp开发者_JS百科ortError: 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
>>>
Are you aware of any issues for Linux/Ubuntu's Python 3.1.2 (Prof. Craven used Windows in his book)? How come Pygame 1.9.1 worked for Python 3.1.3 but not for 3.1.2?
Thanks for any pointers. (--,)
PyGame on 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 may remove the -u 01b2cb16dc17
to try the latest version; 01b2cb16dc17
worked for me.)
I hate to re-open an old post, but I had the hardest time installing pygame with a version of python that was not Ubuntu's default build. So I created this tutorial/ how to:
Install python3.1 and pygame1.9.1 in Ubuntu
I hopes this helps the next unfortunate soul to try this.
I installed pygame for python3 quite easily using the pip3
(a tool for installing and managing Python packages) command on Ubuntu 16.04.7 LTS.
- Open a terminal and install pip3, type
sudo apt install python3-pip
- Now use it to install pygame for python3, type
pip3 install pygame
That's it! Import the library and confirm that everything works:
# I'll try it out using the python 3 interpreter.
python3 --version
Python 3.5.2
robert@robert-E7212:~/Source/Python/python_crash_course/alien_invasion$ python3
Python 3.5.2 (default, Oct 7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
# No errors, pygame was imported successfully!
>>> import pygame
pygame 2.0.0 (SDL 2.0.12, python 3.5.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>>
I followed @Søren 's method, but without the -u number.
The only complication was a few compilation errors at the last line, all due to syntax and unicode differences between Python 2 and Python 3, but with a little checking of the web documentation it was a matter of a few minutes with a text editor modifying the following files (all paths are relative to the pygame directory created during the download):
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
The line numbers from the compiler error messages are great for giving you where to start. The things to look out for are:
1 remove all references to u"xxxx" colours
2 use Python3 syntax for exceptions
3 change all print commands to Python3 equivalents
Then re-issue the final compilation command:
sudo python3 setup.py install
If you miss one or two or get it wrong, just keep going round the loop editing and re-compiling till it works.
BTW I deliberately did not give details of the compiler messages, because I expect they will depend on the current build you download. The files I needed to change were for version '1.9.2pre' downloaded as of the date on this post.
Just use the below command to install pygame for Python3. I could install pygame correctly on Ubuntu 16.04 and Python Python 3.5.2.
pip3 install pygame
It's because installing the python-pygame
package installs it for the default version of Python on your system, 2.6.5 in this case. You should download the pygame package and use setup.py to install it in 3.1.2.
The python-pygame package is only compiled for python2.6 and python2.7 where I am. You'll have to install it again, possibly from a python3 branch of the source.
精彩评论