在 Ubuntu 中安装 Python 3.1.2 的 Pygame
我正在阅读 Paul Craven 所著的《使用 Python 和 Pygame 的计算机科学入门》一书(注:可合法免费在线获取)。在书中,他结合使用了 Python 3.1.3 和 Pygame 1.9.1 。在我的 Linux Ubuntu 机器中,我有 Python 3.1.2,但即使在我 sudo apt-get 安装了 python-pygame (版本 1.9.1)后,Python 3.1.2 也无法导入 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 可以毫无问题地导入它,但是,
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
>>>
您是否知道 Linux/Ubuntu 的 Python 3.1.2 存在任何问题(Craven 教授在他的书中使用了 Windows)?为什么 Pygame 1.9.1 适用于 Python 3.1.3 而不适用于 3.1.2?
感谢您的指点。 (--,)
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>
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
>>>
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. (--,)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Python 3 上的 PyGame 仍处于实验阶段,但这些步骤在 Ubuntu 11.10 上对我有用:(
您可以删除
-u 01b2cb16dc17
以尝试最新版本;01b2cb16dc17
对我有用。 )PyGame on Python 3 remains experimental, but these steps worked for me on Ubuntu 11.10:
(You may remove the
-u 01b2cb16dc17
to try the latest version;01b2cb16dc17
worked for me.)我讨厌重新打开一篇旧帖子,但我在使用非 Ubuntu 默认版本的 python 版本安装 pygame 时遇到了最困难的时刻。所以我创建了这个教程/如何:
安装 python3 Ubuntu 中的 .1 和 pygame1.9.1
我希望这能帮助下一个不幸的人尝试这个。
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.
我在 Ubuntu 16.04.7 上使用
pip3
(用于安装和管理 Python 包的工具)命令非常轻松地安装了 pygame for python3长期支持。sudo apt install python3-pip
pip3 install pygame
就是这样!导入库并确认一切正常:
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.sudo apt install python3-pip
pip3 install pygame
That's it! Import the library and confirm that everything works:
我遵循 @Søren 的方法,但没有 -u 编号。
唯一的复杂之处是最后一行出现了一些编译错误,这一切都是由于 Python 2 和 Python 3 之间的语法和 unicode 差异造成的,但是通过稍微检查一下 Web 文档,只需几分钟即可使用文本编辑器修改以下文件(所有路径都相对于下载期间创建的 pygame 目录):
编译器错误消息中的行号非常适合为您提供起点。需要注意的是:
然后重新发出最终的编译命令:
如果你错过了一两个或者弄错了,就继续循环编辑并重新编译,直到它起作用为止。
顺便说一句,我故意没有提供编译器消息的详细信息,因为我预计它们将取决于您下载的当前版本。我需要更改的文件是截至本文发布之日下载的版本“1.9.2pre”。
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):
The line numbers from the compiler error messages are great for giving you where to start. The things to look out for are:
Then re-issue the final compilation command:
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.
只需使用以下命令安装 Python3 的 pygame。我可以在 Ubuntu 16.04 和 Python Python 3.5.2 上正确安装 pygame。
pip3安装pygame
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
这是因为安装
python-pygame
包会在您的系统上安装默认版本的 Python,在本例中为 2.6.5。您应该下载 pygame 包并使用 setup.py 在 3.1.2 中安装它。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.python-pygame 包仅针对我所在的 python2.6 和 python2.7 进行编译。您必须再次安装它,可能是从源代码的 python3 分支安装。
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.