【作業メモ】MacでPython3環境を整える
MacでPython3を使うために環境設定したメモ。
環境
- macOS Mojave
pyenvのインストール
pyenvは複数のバージョンのPythonを用意に切り替えることができるツール。複数バージョンを利用することがなければ、特に必要なし。その場合はbrew install python3
だけで環境の用意は終わり。
$ brew update
$ brew install pyenv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
phpstan
==> Installing dependencies for pyenv: openssl and readline
==> Installing pyenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2r.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/c1/c1f8c06740398325c7028213b20b18c5de39763fbc81e5819c78a06ee0621170?__gda__=exp=1557763580~hmac=fd3d999e884111c8cfd5166adb4cbe998c39193dc46575462b4eb2cd982d
######################################################################## 100.0%
==> Pouring openssl-1.0.2r.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"' >> ~/.zshrc
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"
==> Summary
🍺 /usr/local/Cellar/openssl/1.0.2r: 1,795 files, 12.1MB
==> Installing pyenv dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/fa/faab004773e6449dd97971311cb62a9bbaa44f1483b82640e818f0c355c8266d?__gda__=exp=1557763588~hmac=620bcc61e0186c035ba6c3283990594694b3b18e418177395e4ea183f64d
######################################################################## 100.0%
==> Pouring readline-8.0.0_1.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"
For pkg-config to find readline you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"
==> Summary
🍺 /usr/local/Cellar/readline/8.0.0_1: 48 files, 1.5MB
==> Installing pyenv
==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.2.11.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/54/54876153bcbe6bfdecd0b232ab112571120c84ae8c11e610c07775026e403818?__gda__=exp=1557763590~hmac=cd7e80e6cc38d9c159df80dc665b6a9f0b791e8d707313dc379fefb77a18
######################################################################## 100.0%
==> Pouring pyenv-1.2.11.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/pyenv/1.2.11: 643 files, 2.4MB
==> Caveats
==> 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"' >> ~/.zshrc
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"
For pkg-config to find readline you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"
pyenvのバージョンを確認できたらOK。
$ pyenv -v
pyenv 1.2.11
pyenvのパスを通しておく。これをしないとpyenvでバージョンを切り替えても実際に実行されるpythonはそのまま。
~/.zshrc
(Bash利用の人は~/.bashrc
)に追記。
# ~/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
ファイルを読み込み直す。
$ source ~/.zshrc
Python3のインストール
まず現在インストールされているPythonバージョン一覧と現在設定利用しているPythoのバージョンを確認。
$ pyenv versions
* system
$ python --version
Python 2.7.15
インストールできるPythonバージョンの一覧を確認。
$ pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
...
バージョン3.7.3
をインストール。
$ pyenv install 3.7.3
下記のようなzlib
のエラーが出た時はzlib
をインストールあるいはアップデートして、再度Pythonをインストール。
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
$ brew install zlib
zlib
をインストールで解決ぜず、macOS Mojaveを利用している場合はSDKが足りない可能性がある。
pyenvのissueで解決策が報告されている。
Install failed, "zlib not available" on macOS Mojave
以下のコマンドを実行。
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
インストール済みバージョンを確認して3.7.3
が表示されればOK。
$ pyenv versions
* system
3.7.3
あとはバージョンを切り替えるだけ。全ての環境でバージョンを変える場合はpyenv global
を、指定のディレクトリ以下でのみバージョンを変える場合はそのディレクトリでpyenv local
を使う。
$ pyenv global 3.7.3
$ pyenv versions
system
* 3.7.3
$ python --version
Python 3.7.3