acokikoy's notes

{"勉強中":"Python","注目":"Shopify","LOVE♡":["ABARTH595","TA-GG9","Ukulele","Movable Type","ガーナ ミルクチョコレート"]} なWebディレクター

Pygame Zero インストール時トラブルの解決メモ( macOS Catalina, Python3.8.0)

日経ソフトウェア2020年5月号 の付録「5日でできる!Pythonでゲーム作成入門」を試そうとしたところ、しょっぱなから Pygame Zero インストールで躓いた。その時の解決メモ。

pip install pgzero でエラー。SDL.h がないとしかられる

現象
pip install pgzero 途中でエラー発生。
"SDL.h がみつからない" と叱られてインストールが完了しない。

% pip install pgzero
Collecting pgzero
  Downloading pgzero-1.2-py3-none-any.whl (69 kB)
     |████████████████████████████████| 69 kB 2.4 MB/s 
Collecting numpy
  Downloading numpy-1.18.2-cp38-cp38-macosx_10_9_x86_64.whl (15.2 MB)
     |████████████████████████████████| 15.2 MB 7.6 MB/s 
Collecting pygame<2.0,>=1.9.2
  Downloading pygame-1.9.6.tar.gz (3.2 MB)
     |████████████████████████████████| 3.2 MB 12.1 MB/s 
Installing collected packages: numpy, pygame, pgzero
    Running setup.py install for pygame ... error
    ERROR: Command errored out with exit status 1:
     command: /Users/acokikoy/.pyenv/versions/3.8.0/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-install-0o6n1il8/pygame/setup.py'"'"'; __file__='"'"'/private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-install-0o6n1il8/pygame/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-record-057ov5_y/install-record.txt --single-version-externally-managed --compile --install-headers /Users/acokikoy/.pyenv/versions/3.8.0/include/python3.8/pygame
         cwd: /private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-install-0o6n1il8/pygame/
    Complete output (218 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.15-x86_64-3.8
    creating build/lib.macosx-10.15-x86_64-3.8/pygame
    copying src_py/surfarray.py -> build/lib.macosx-10.15-x86_64-3.8/pygame

    ...

    running build_ext
    building 'pygame.gfxdraw' extension
    creating build/temp.macosx-10.15-x86_64-3.8
    creating build/temp.macosx-10.15-x86_64-3.8/src_c
    creating build/temp.macosx-10.15-x86_64-3.8/src_c/SDL_gfx
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DENABLE_NEWBUF=1 -I/NEED_INC_PATH_FIX -I/Users/acokikoy/.pyenv/versions/3.8.0/include/python3.8 -c src_c/gfxdraw.c -o build/temp.macosx-10.15-x86_64-3.8/src_c/gfxdraw.o
    In file included from src_c/gfxdraw.c:33:
    In file included from src_c/pygame.h:32:
    src_c/_pygame.h:216:10: fatal error: 'SDL.h' file not found
    #include <SDL.h>
             ^~~~~~~
    1 error generated.
    ---
    For help with compilation see:
        https://www.pygame.org/wiki/MacCompile
    To contribute to pygame development see:
        https://www.pygame.org/contribute.html
    ---
    error: command 'clang' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/acokikoy/.pyenv/versions/3.8.0/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-install-0o6n1il8/pygame/setup.py'"'"'; __file__='"'"'/private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-install-0o6n1il8/pygame/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/mz/wjwxp0m962q_x06kwrdbfnr80000gn/T/pip-record-057ov5_y/install-record.txt --single-version-externally-managed --compile --install-headers /Users/acokikoy/.pyenv/versions/3.8.0/include/python3.8/pygame Check the logs for full command output.

エラーメッセージに 'SDL.h' file not found などとある。
Stack Overflow の近い事例を参考に、不足しているパッケージをインストールしたらうまく行った。

参照した文献(Stack Overflow):
Can't install pygame with pip as there is an error whilst runningvsetup.py bdist_wheel for pygame - Stack Overflow

% brew install sdl sdl_image sdl_mixer sdl_ttf portmidi
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
confd            gobo             inxi             llvm@9           minipro          semtag           vroom
==> Updated Formulae
...
==> Updated Casks
...
==> Deleted Casks
...
==> Downloading https://homebrew.bintray.com/bottles/sdl-1.2.15_1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/77/7711f995186b4aa6dff3f9821be99bbb1c455b32d353adf6b2fd5ec6404a52a3?__g
######################################################################## 100.0%
==> Pouring sdl-1.2.15_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/sdl/1.2.15_1: 225 files, 1.4MB
==> Installing dependencies for sdl_image: jpeg, libpng, libtiff and webp
==> Installing sdl_image dependency: jpeg
==> Downloading https://homebrew.bintray.com/bottles/jpeg-9d.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/8f/8f7b82a952fb3937889c7f22da1403e5338cd320495917eb26b0c5b2e614791c?__g
######################################################################## 100.0%
==> Pouring jpeg-9d.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/jpeg/9d: 21 files, 775.2KB
==> Installing sdl_image dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.37.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/c8/c8e74da602c21f978cd7ee3d489979b4fc6681e71f678a1d99012943ee3a909f?__g
######################################################################## 100.0%
==> Pouring libpng-1.6.37.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.37: 27 files, 1.2MB
==> Installing sdl_image dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.1.0.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/44/449bd9123e73e4c4eab85b77322d769cc9df0f6adab05e9b9319b012d1215a68?__g
######################################################################## 100.0%
==> Pouring libtiff-4.1.0.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libtiff/4.1.0: 247 files, 3.7MB
==> Installing sdl_image dependency: webp
==> Downloading https://homebrew.bintray.com/bottles/webp-1.1.0.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/27/27c76a7054277ff5a2e844c5996fc731d8644acbaaa505d35dba42c4a48a0c51?__g
######################################################################## 100.0%
==> Pouring webp-1.1.0.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/webp/1.1.0: 39 files, 2.1MB
==> Installing sdl_image
==> Downloading https://homebrew.bintray.com/bottles/sdl_image-1.2.12_7.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sdl_image-1.2.12_7.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/sdl_image/1.2.12_7: 10 files, 139.4KB
==> Installing dependencies for sdl_mixer: libmikmod, libogg and libvorbis
==> Installing sdl_mixer dependency: libmikmod
==> Downloading https://homebrew.bintray.com/bottles/libmikmod-3.3.11.1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/21/2151c9e70ca92a911af8cf769c18541c5d107df349b44987f716909c67216c59?__g
######################################################################## 100.0%
==> Pouring libmikmod-3.3.11.1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libmikmod/3.3.11.1: 15 files, 799.8KB
==> Installing sdl_mixer dependency: libogg
==> Downloading https://homebrew.bintray.com/bottles/libogg-1.3.4.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/0a/0a03b8a7307aeac70762fd4ee9837fff4ed523c34063a6aec52c5cf34c54695f?__g
######################################################################## 100.0%
==> Pouring libogg-1.3.4.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libogg/1.3.4: 97 files, 476.9KB
==> Installing sdl_mixer dependency: libvorbis
==> Downloading https://homebrew.bintray.com/bottles/libvorbis-1.3.6.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/89/893672680486cd93229556f0dc573d06bf5b21061193580e3b0a21b5b4ae4e40?__g
######################################################################## 100.0%
==> Pouring libvorbis-1.3.6.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libvorbis/1.3.6: 157 files, 2.3MB
==> Installing sdl_mixer
==> Downloading https://homebrew.bintray.com/bottles/sdl_mixer-1.2.12_3.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sdl_mixer-1.2.12_3.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/sdl_mixer/1.2.12_3: 10 files, 416.6KB
==> Installing dependencies for sdl_ttf: freetype
==> Installing sdl_ttf dependency: freetype
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.10.1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/dd/ddd686141a969caec11ea248324e3736f6db50a54673187be103dde39cb01ebf?__g
######################################################################## 100.0%
==> Pouring freetype-2.10.1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/freetype/2.10.1: 61 files, 2.2MB
==> Installing sdl_ttf
==> Downloading https://homebrew.bintray.com/bottles/sdl_ttf-2.0.11_1.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sdl_ttf-2.0.11_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/sdl_ttf/2.0.11_1: 10 files, 75.1KB
==> Downloading https://homebrew.bintray.com/bottles/portmidi-217_2.catalina.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring portmidi-217_2.catalina.bottle.1.tar.gz
🍺  /usr/local/Cellar/portmidi/217_2: 8 files, 89.8KB
(pygame_zero) [acokikoy@mbp2018acokikoy] ~/projects/Python_de_game/pygame_zero
% pip install pgzero                                   
Collecting pgzero
  Using cached https://files.pythonhosted.org/packages/48/e5/e5f14292373cb5fc7539aa01307b184c1e3c954d68945d8c44778669dd82/pgzero-1.2-py3-none-any.whl
Collecting pygame<2.0,>=1.9.2 (from pgzero)
  Using cached https://files.pythonhosted.org/packages/0f/9c/78626be04e193c0624842090fe5555b3805c050dfaa81c8094d6441db2be/pygame-1.9.6.tar.gz
Requirement already satisfied: numpy in ./lib/python3.8/site-packages (from pgzero) (1.18.2)
Installing collected packages: pygame, pgzero
  Running setup.py install for pygame ... done
Successfully installed pgzero-1.2 pygame-1.9.6
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

再度インストール実行。無事 pgzero がインストールできた。

% pip install pgzero
Collecting pgzero
  Using cached pgzero-1.2-py3-none-any.whl (69 kB)
Collecting pygame<2.0,>=1.9.2
  Using cached pygame-1.9.6.tar.gz (3.2 MB)
Requirement already satisfied: numpy in ./.pyenv/versions/3.8.0/lib/python3.8/site-packages (from pgzero) (1.18.2)
Installing collected packages: pygame, pgzero
    Running setup.py install for pygame ... done
Successfully installed pgzero-1.2 pygame-1.9.6

pgzrun.go()すると、ウインドウが起動するものの、白い画面でなにも描画されない

現象
images/alien.png を描画するだけのスクリプトで、 真っ白いウインドウが出るものの、なにも描画されない。 雑誌掲載のキャプチャ画面は背景黒のウインドウなのに、こっちは真っ白の背景というのもおかしい。

# エイリアンの画像を描画するだけのサンプルスクリプト
# 画像の所在は images/alien.png
import pgzrun

WIDTH = 200
HEIGHT = 200

alien = Actor('alien', center=(100,100))

def draw():
    screen.clear()
    alien.draw()

pgzrun.go()

どうやら MacOS Mojave と現stableバージョン間で互換がないらしい。
v2.0.0 dev版最新バージョンをインストールすることで解決した。

% pip install pygame==2.0.0.dev6

参照した文献(GitHubpygameプロジェクトissues):
Pygame not compatible with MacOS Mojave · Issue #555 · pygame/pygame

最新のdevバージョンはここにある。
pygame 2.0.0.dev6 on PyPI - Libraries.io

無事にエイリアンが表示された♡

無事にかわいいエイリアンが表示された♡

f:id:acokikoy:20200403091524p:plain)

Pygame Zeroの使い方はここ(ちょっとバージョンが古いが読みやすい)
Pygame Zero へようこそ — Pygame Zero 1.2 ドキュメント