pythonではpyenvを使うことで、python2系とpython3系を共存させることができます。
今回は、このpyenvをmacOSにインストールして、別バージョンのpythonをインストールしてみました。
実行環境
macOS High Sierra10.13.4
brewで、gitコマンドインストール済み
実行するコマンド
以下のコマンドを順に実行することで、インストール/別バージョンのpythonインストール/バージョンの切り替えまで行えます。
# get pyenv
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
# edit bash_profile
echo '' >> ~/.bash_profile
echo '#pyenv' >> ~/.bash_profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# check bash_profile
cat ~/.bash_profile
# enable bash_profile
source ~/.bash_profile
# install latest 3.6.x
pyenv install --list
pyenv install 3.6.5
# check installed
pyenv versions
# change python version
pyenv rehash
pyenv global 3.6.5
# check python version
pyenv versions
python --version
which python
実行結果のログ
$ python --version
Python 2.7.14
$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/Users/xxx/.pyenv'...
remote: Counting objects: 16344, done.
remote: Compressing objects: 100% (70/70), done.
remote: Total 16344 (delta 57), reused 85 (delta 42), pack-reused 16226
Receiving objects: 100% (16344/16344), 3.14 MiB | 712.00 KiB/s, done.
Resolving deltas: 100% (11099/11099), done.
$ echo '' >> ~/.bash_profile
$ echo '#pyenv' >> ~/.bash_profile
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
#pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
$ pyenv
-bash: pyenv: command not found
$ source ~/.bash_profile
$ pyenv
pyenv 1.2.4-1-ga2d5132
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
$ pyenv versions
* system (set by /Users/xxx/.pyenv/version)
$ pyenv install --list
Available versions:
2.1.3
2.2.3
...
3.6.0
3.6-dev
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.7.0b4
3.7-dev
3.8-dev
...
$ pyenv install 3.6.5
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.5.tar.xz...
-> https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
Installing Python-3.6.5...
python-build: use readline from homebrew
Installed Python-3.6.5 to /Users/xxx/.pyenv/versions/3.6.5
$ pyenv versions
* system (set by /Users/xxx/.pyenv/version)
3.6.5
$ pyenv rehash
$ pyenv global 3.6.5
$ pyenv versions
system
* 3.6.5 (set by /Users/xxx/.pyenv/version)
$ python --version
Python 3.6.5
$ which python
/Users/xxx/.pyenv/shims/python
こちらもおススメ