Web制作・開発会社 プレスマンのスタッフブログ

PRESSMAN*Tech

pyenv で Python のインストールができない

pyenv がエラーを出す

あるとき、pyenv コマンドで Python をインストールしようとしたところ、エラーを出すようになっていました。
環境は macOS Mojave ですが、こちらの指示どおり SDK ヘッダーのインストールも実施済みです。

When running Mojave or higher (10.14+) you will also need to install the additional SDK headers by downloading them from Apple Developers. You can also check under /Library/Developer/CommandLineTools/Packages/ as some versions of Mac OS will have the pkg locally.

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

実際に実行してみると以下のようになります。

$ pyenv install 3.6.6
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.14.2 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/y3/ycyymmyx7jg5kbyx3f8cd8c80000gp/T/python-build.20181225105714.5291
Results logged to /var/folders/y3/ycyymmyx7jg5kbyx3f8cd8c80000gp/T/python-build.20181225105714.5291.log

Last 10 log lines:
make: *** [Parser/listnode.o] Error 1
make: *** [Parser/grammar1.o] Error 1
1 error generated.
1 error generated.
make: *** [Parser/parser.o] Error 1
1 error generated.
make: *** [Parser/bitset.o] Error 1
1 error generated.
make: *** [Parser/metagrammar.o] Error 1
make: *** [Parser/node.o] Error 1 

出力されたログを見てみる

ログは以下のようになっていました。

/var/folders/y3/ycyymmyx7jg5kbyx3f8cd8c80000gp/T/python-build.20181225105714.5291 ~
/var/folders/y3/ycyymmyx7jg5kbyx3f8cd8c80000gp/T/python-build.20181225105714.5291/Python-3
.6.6 /var/folders/y3/ycyymmyx7jg5kbyx3f8cd8c80000gp/T/python-build.20181225105714.5291 ~
checking build system type... x86_64-apple-darwin18.2.0
checking host system type... x86_64-apple-darwin18.2.0
<中略>
In file included from Parser/metagrammar.c:2:
In file included from ./Include/pgenheaders.h:10:
In file included from ./Include/Python.h:78:
./Include/bytearrayobject.h:9:10: fatal error: 'stdarg.h' file not found
#include <stdarg.h>
         ^~~~~~~~~~
In file included from Parser/node.c:3:
In file included from ./Include/Python.h:78:
./Include/bytearrayobject.h:9:10: fatal error: 'stdarg.h' file not found
#include <stdarg.h>
         ^~~~~~~~~~
1 error generated.
1 error generated.
1 error generated.
make: *** [Programs/python.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [Parser/acceler.o] Error 1
1 error generated.
make: *** [Parser/listnode.o] Error 1
make: *** [Parser/grammar1.o] Error 1
1 error generated.
1 error generated.
make: *** [Parser/parser.o] Error 1
1 error generated.
make: *** [Parser/bitset.o] Error 1
1 error generated.
make: *** [Parser/metagrammar.o] Error 1
make: *** [Parser/node.o] Error 1

stdarg.h が無いとか言われてます。

本当に stdarg.h が存在しないのか確認

ファイル検索をかけてみると

$ mdfind stdarg.h
/Users/yosuke/emsdk/emscripten/1.38.11/system/lib/libc/musl/arch/emscripten/bits/stdarg.h
/Users/yosuke/emsdk/emscripten/1.38.11/system/include/libc/stdarg.h
/System/Library/PrivateFrameworks/GPUCompiler.framework/Versions/A/Libraries/lib/clang/3.5/include/stdarg.h
/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/stdarg.h
/usr/include/c++/4.2.1/tr1/stdarg.h
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/Clang/include/stdarg.h
/Library/Developer/CommandLineTools/usr/lib/clang/10.0.0/include/stdarg.h
/Library/Developer/CommandLineTools/usr/lib/llvm-gcc/4.2.1/include/stdarg.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/stdarg.h
/Library/Developer/CommandLineTools/usr/lib/tapi/10.0.0/include/stdarg.h
/Users/yosuke/Library/Android/sdk/build-tools/26.0.2/renderscript/clang-include/stdarg.h
/Users/yosuke/emsdk/emscripten/1.38.11/system/include/compat/stdarg.h

ありました。というより多すぎる。コレが原因かも?

emsdk のパスを外してみる

emsdk は以前に投稿した下記の記事を作成した際にインストールしたものです。

https://www.pressmantech.com/tech/programming/5042

~/.bashrcに以下を追記

source ~/emsdk/emsdk_env.sh > /dev/null

上記をコメントアウト後、シェルを再起動して pip install してみます。

$ pyenv install 3.6.6
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew
Installed Python-3.6.6 to /Users/yosuke/.pyenv/versions/3.6.6

インストールできました!

まとめ

ちょうどOSをアップグレードしたタイミングだったため、「Mojave pyenv」とかでググってあれこれやってたのですが、まったく関係ないところで躓いていたというオチでした。
同じように躓いている人の参考になれば幸いです。
以上、唐突な Python ネタでした。