京橋のバイオインフォマティシャンの日常

データ分析、コマンドライン、プログラミングについての技術資料・自己アップデート・悩み事などをまとめています。最近、ディープラーニング関連のR言語の資料をまとめるべく注力してます。

Mac brew で、pythonの古いバージョンを管理するときのTips 〜「python3」から「python」にコマンド名を書き換える〜

pythonの古いバージョン(例えば、3.8とか)をインストールしたいとき、 パッケージのlinkをしないと、それが使えなくなっている。

今回、python3.8をbrewでインストールして、パッケージのlinkをやり直して、 コマンド名を「python」に書き換えるまでをやってみる。

#pythonのパス確認 => /usr/bin内のpythonが参照されている。
$ which python
/usr/bin/python

#python@3.8のインストール
$ brew install python@3.8

#...
#...
#==> Summary
#🍺  /usr/local/Cellar/tcl-tk/8.6.11_1: 3,041 files, 51.6MB
#==> Installing python@3.8
#==> Pouring python@3.8-3.8.7_2.catalina.bottle.tar.gz
#==> /usr/local/Cellar/python@3.8/3.8.7_2/bin/python3 -s setup.py --no-user-cfg i
#==> /usr/local/Cellar/python@3.8/3.8.7_2/bin/python3 -s setup.py --no-user-cfg i
#==> /usr/local/Cellar/python@3.8/3.8.7_2/bin/python3 -s setup.py --no-user-cfg i
#==> Caveats
#Python has been installed as
#  /usr/local/opt/python@3.8/bin/python3
#
#Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
#`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
#  /usr/local/opt/python@3.8/libexec/bin
#
#You can install Python packages with
#  /usr/local/opt/python@3.8/bin/pip3 install <package>
#They will install into the site-package directory
#  /usr/local/lib/python3.8/site-packages
#...

ただ、これだけでは、pythonがアップデートされていない。

#pythonのパス確認
$ which python
/usr/bin/python

#python3のパス確認
$ which python3
/usr/bin/python3

そこで、python@3.8へとlinkを書き換える。

そうすると、/usr/local/bin内の、python3が参照される。

#リンクの上書き
$ brew link --overwrite python@3.8

#パス確認
$ which python3
/usr/local/bin/python3

次に、/usr/local/bin内で、pythonとpip関連コマンドのエイリアス名を書き換える。

#pythonとpip関連コマンドの表示
$ ls /usr/local/bin | grep -e "py" -e "pip"

#pip3
#pip3.8
#pydoc3
#pydoc3.8
#python3
#python3-config
#python3.8
#python3.8-config

#エイリアス名の書き換え
mv /usr/local/bin/pip3 /usr/local/bin/pip
mv /usr/local/bin/pydoc3 /usr/local/bin/pydoc
mv /usr/local/bin/python3 /usr/local/bin/python
mv /usr/local/bin/python3-config /usr/local/bin/python-config 

上記を行うと、python、pipコマンドがそれぞれ、python3.8と紐づけられる。

$ which python
/usr/local/bin/python

$ python -V
Python 3.8.7

$ which pip
/usr/local/bin/pip

$ pip -V
pip 20.3.3 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

以上、おしまい。

過去の関連記事は、以下を参照のこと。

skume.net

補足

2021年3月4日現在、brew infoで調べたところ、python@3.7以上しか、リポジトリにはないらしい。

$ brew info python@3.6

Error: No available formula or cask with the name "python@3.6".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.


$ brew info python@3.7

python@3.7: stable 3.7.10 (bottled) [keg-only]
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python@3.7.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✔, mpdecimal ✘, openssl@1.1 ✔, readline ✔, sqlite ✔, xz ✔
==> Requirements
Required: x86_64 architecture ✔
==> Caveats
Python has been installed as
  /usr/local/opt/python@3.7/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python@3.7/libexec/bin

You can install Python packages with
  /usr/local/opt/python@3.7/bin/pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

python@3.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

==> Analytics
install: 33,684 (30 days), 73,067 (90 days), 197,222 (365 days)
install-on-request: 27,647 (30 days), 63,841 (90 days), 141,223 (365 days)
build-error: 0 (30 days)


$ brew info python@3.8

python@3.8: stable 3.8.8 (bottled) [keg-only]
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python@3.8.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✔, mpdecimal ✘, openssl@1.1 ✔, readline ✔, sqlite ✔, tcl-tk ✔, xz ✔
==> Caveats
Python has been installed as
  /usr/local/opt/python@3.8/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python@3.8/libexec/bin

You can install Python packages with
  /usr/local/opt/python@3.8/bin/pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

python@3.8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

==> Analytics
install: 348,989 (30 days), 851,949 (90 days), 4,803,149 (365 days)
install-on-request: 187,812 (30 days), 509,836 (90 days), 1,521,919 (365 days)
build-error: 0 (30 days)