初期状態のシステムのpythonだとpipインストール等で問題が発生する事があるので、pyenv環境に変えます。
1 2 |
$ which python /usr/bin/python |
↑初期状態のパス
brewでpyenvインストール
1 2 |
$ brew update $ brew install pyenv |
.bash_profileに下記を追加してsource
1 2 3 4 5 |
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)" $ source .bash_profile |
バージョンを指定してpythonをインストール
1 2 3 4 5 |
$ pyenv install 2.7.11 ERROR: The Python zlib extension was not compiled. Missing the zlib? というエラーが出る場合は↓ $CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 2.7.11 |
パスが切り替わっている事を確認
1 2 3 4 5 |
$ which python /Users/xxx/.pyenv/shims/python $ which pip /Users/xxx/.pyenv/shims/pip |
使用するpythonを設定
1 2 3 4 5 6 7 |
$ pyenv versions * system (set by /Users/xxx/.pyenv/version) 2.7.11 $ pyenv global 2.7.11 $ pyenv versions system * 2.7.11 (set by /Users/xxx/.pyenv/version) |
追記
high Sierraで pyenv install してopensslがどうとかいうエラーになる場合、
1 |
$ CFLAGS="-I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib" pyenv install -v 3.6.0 |