2019年1月3日木曜日

macOS Mojave 10.14.2にPython3.7.1インストール



前回、Homebrewをインストールしたのでやっとpythonをインストール


  • macOS Mojave 10.14.2
  • Python3.7.1

・pyenvのインストール

今のところ複数のバージョンを切り替えて使う予定がないのでpyenvのインストールは不要あったかもしれないけれど今後のために。

(インストールコマンド)
$brew install pyenv

(インストールログ)
brew install pyenv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
atomist-cli                               jmxterm                                   ruby@2.5
==> Updated Formulae
algernon                       dub                            libcds                         re2
ballerina                      dvm                            libhttpserver                  roswell
bullet                         easyengine                     libjwt                         swi-prolog
checkstyle                     efl                            librealsense                   swiftformat
clojure                        fbi-servefiles                 meson                          syncthing
cointop                        gexiv2                         mono                           tika
dartsim                        global                         neovim                         topgrade
dcd                            gobject-introspection          nnn                            unnethack
dependency-check               grakn                          node-build                     verilator
dfmt                           groovy                         pagmo                          wtf
diamond                        http-parser                    pgbadger                       ydcv
diffutils                      imagemagick                    php-cs-fixer                   youtube-dl
docker-machine-parallels       kops                           picard-tools
dscanner                       ldc                            plantuml
==> Deleted Formulae
pyexiv2

==> Installing dependencies for pyenv: autoconf, openssl, pkg-config and readline
==> Installing pyenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.mojave.bottle.4.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.mojave.bottle.4.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
瑳  /usr/local/Cellar/autoconf/2.69: 71 files, 3.0MB
==> Installing pyenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2q.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2q.mojave.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ‾/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
瑳  /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.1MB
==> Installing pyenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.2.mojave.bottle.tar.gz
瑳  /usr/local/Cellar/pkg-config/0.29.2: 11 files, 627.2KB
==> Installing pyenv dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.5.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-7.0.5.mojave.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

==> Summary
瑳  /usr/local/Cellar/readline/7.0.5: 46 files, 1.5MB
==> Installing pyenv
==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.2.8_1.mojave.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring pyenv-1.2.8_1.mojave.bottle.1.tar.gz
瑳  /usr/local/Cellar/pyenv/1.2.8_1: 612 files, 2.4MB
==> Caveats
==> autoconf
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ‾/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

For pkg-config to find openssl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"


(パス追加)
pyenvのパスを通すために.bash_profileに追加
※自分の環境には.bash_profileは存在しておらず今回新規に作成

・.bash_profile作成
$touch ~/.bash_profile

・パス追加
.bash_profileに以下を追加
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

・.bash_profileの読み込み
パスに追加しても次回のシェル起動時からしか有効にならないので手動で.bash_profileを読み込ませる

$ source ~/.bash_profile

・pythonのインストール


pyenvでインストール出来るバージョンを確認する

$ pyenv install --list

anacondaやjpythonなども表示するけれど数字だけのがpythonのことのようで、今回は現時点で最新の3.7.1をインストールすることに

$ pyenv install 3.7.1

(インストールログ)
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.1.tar.xz...
-> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
Installing Python-3.7.1...
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/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103125847.9526
Results logged to /var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103125847.9526.log

Last 10 log lines:
rm -f ./pybuilddir.txt ; \
exit 1 ; \
fi
Fatal Python error: init_sys_streams: can't initialize sys standard streams
LookupError: unknown encoding: SJIS

Current thread 0x000000011701a5c0 (most recent call first):
/bin/sh: line 1: 18246 Abort trap: 6           ./python.exe -E -S -m sysconfig --generate-posix-vars
generate-posix-vars failed
make: *** [pybuilddir.txt] Error 1

うーん、とりあえずエンコーディングのSJISを知らないと・・・ターミナルのテキストエンコーディングをUTF-8にしてリトライ
※そもそも~(チルダ)を入力できず問題でエンコーディングをUTF-8に変更はしていたけれどターミナルを開きなおしていなかったのでシェルとしてはSJISのままだった。

(インストールログ)
pyenv install 3.7.1
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.1.tar.xz...
-> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
Installing Python-3.7.1...
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/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567
Results logged to /var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567.log

Last 10 log lines:
  File "/private/var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567/Python-3.7.1/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567/Python-3.7.1/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567/Python-3.7.1/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/18/0w69zlln2v54_mhnf6s014dc0000gn/T/python-build.20190103130926.18567/Python-3.7.1/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

調べてみるとMojave用のmacOS SDK headerがインストールされていないかららしく手動でインストールしてみることに

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

(インストールログ)
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
Password:
installer: Package name is macOS_SDK_headers_for_macOS_10.14
installer: Installing at base path /

installer: The install was successful.

再度python3.7.1をインストールして無事成功
(インストールログ)
$ pyenv install 3.7.1
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.1.tar.xz...
-> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
Installing Python-3.7.1...
python-build: use readline from homebrew

Installed Python-3.7.1 to /Users/[ユーザid hogehoge]/.pyenv/versions/3.7.1

・pythonバージョンの切り替え

インストールしただけではデフォルトのpythonはもともとインストールされているバージョンが使用されるので切り替える

$ python --version
Python 2.7.10
$ pyenv global 3.7.1
$ pyenv versions
  system

* 3.7.1 (set by /Users/[ユーザid hogehoge]/.pyenv/version)
$ python --version
Python 3.7.1

※選ばれているバージョンの前に*が表示しているっぽい

ちなみにアンインストール時は
$ pyenv uninstall 3.7.1

参考サイト








0 件のコメント:

コメントを投稿