簡體   English   中英

在 Ubuntu 中為 Python 3.1.2 安裝 Pygame

[英]Installing Pygame for Python 3.1.2 in Ubuntu

我正在閱讀 Paul Craven 的《使用 Python 和 Pygame 的計算機科學導論》一書(注意:可合法免費在線獲得)。 在書中,他使用了 Python 3.1.3 和 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 毫不費力地導入它,但是,

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?

感謝您的任何指示。 (--,)

Python 3 上的 PyGame 仍處於試驗階段,但這些步驟在 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

(您可以刪除-u 01b2cb16dc17以嘗試最新版本; 01b2cb16dc17為我工作。)

我討厭重新打開一個舊帖子,但我最難安裝 pygame 版本的 python 不是 Ubuntu 的默認版本。 所以我創建了本教程/如何:

在Ubuntu中安裝python3.1和pygame1.9.1

我希望這有助於下一個不幸的靈魂嘗試這個。

我在 Ubuntu L604 上使用pip3用於安裝和管理 Python 包的工具)命令很容易地為 python3安裝了pygame

  1. 打開終端並安裝pip3 ,輸入sudo apt install python3-pip
  2. 現在用它為python3安裝pygame ,輸入pip3 install pygame

就是這樣:導入庫並確認一切正常:

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

我遵循@Søren 的方法,但沒有 -u 號碼。

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以下文件(所有路徑都相對於下載期間創建的 pygame 目錄):

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 

編譯器錯誤消息中的行號非常適合為您提供從哪里開始。 需要注意的事項是:

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

然后重新發出最終的編譯命令:

sudo python3 setup.py install

如果你錯過了一兩個或弄錯了,只需繼續循環編輯並重新編譯直到它工作。

順便說一句,我故意沒有提供編譯器消息的詳細信息,因為我希望它們將取決於您下載的當前版本。 我需要更改的文件是截至本文發布之日下載的“1.9.2pre”版本。

只需使用以下命令為 Python3 安裝 pygame。 I could install pygame correctly on Ubuntu 16.04 and Python Python 3.5.2.

pip3 安裝 pygame

這是因為在您的系統上安裝python-pygame package 會為默認版本的 Python 安裝它,在這種情況下為 2.6.5。 您應該下載 pygame package 並使用 setup.py 在 3.1.2 中安裝它。

python-pygame package 僅針對我所在的 python2.6 和 python2.7 編譯。 您必須再次安裝它,可能來自源代碼的 python3 分支。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM