3
1

More than 3 years have passed since last update.

MacOS CatalinaでPython3をインストールする(Homebrewのみで)

Last updated at Posted at 2020-08-06

はじめに

この記事でpyenvを利用してPython3をインストールする方法をまとめましたが、
pyenvを利用しないでHomebrewのみでインストールする方法もあったので勉強を兼ねてやってみました。

実行環境

  • MacBookPro Mid 2014
  • macOS Catalina ver.10.15.16

使用するパッケージ

  • Homebrew

    • 言わずと知れたMacとLinux向けのパッケージ管理ツール。今回はこれだけで完結します。

※ 詳しい説明は公式ドキュメントを参照して下さい。

インストールの流れ

  1. Homebrewをインストール
  2. Pythonをインストール

1.Homebrewをインストール

公式ドキュメントに従ってターミナルで以下のコマンドを実行します。インストール済みなら飛ばしても問題ありません。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

インストールが成功していればbrew -vコマンドでHomebrewのバージョンが確認できます。

$ brew -v
Homebrew 2.4.9
Homebrew/homebrew-core (git revision 3b87b; last commit 2020-08-04)
Homebrew/homebrew-cask (git revision 2ee9f; last commit 2020-08-04)

2.Pythonをインストール

Homebrewを最新の状態に更新してからインストールします。
brewコマンドで最新の状態に更新します。

$ brew update

Homebrewが更新できたらbrew installでPython3をインストールします。

$ brew install python

brew listでインストールしたパッケージを確認できます。

$ brew list
etc...
python@3.8
etc...

これでPython3のインストールは完了しましたが、システムがデフォルトで認識しているのはPython2のままです。

そのためpythonpipコマンドはPython2で実行されるので、Python3を実行するにはpython3pip3と入力しないと認識されません。

$ python -V
Python 2.7.16

$ python3 -V
Python 3.8.5


$ pip -V
zsh: command not found: pip

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

まぁ動くのでそれでもいいといえばいいのですが、面倒なのでPATHを通しておきます。

brew infoコマンドでPython3がインストールされたディレクトリを確認します。

$ brew info python
python@3.8: stable 3.8.5 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python@3.8/3.8.5 (4,339 files, 67.3MB) *
  Poured from bottle on 2020-08-05 at 17:48:14
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python@3.8.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✔, openssl@1.1 ✔, readline ✔, sqlite ✔, xz ✔
==> Caveats
Python has been installed as
  /usr/local/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
  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
==> Analytics
install: 749,693 (30 days), 1,531,213 (90 days), 2,458,114 (365 days)
install-on-request: 200,840 (30 days), 225,547 (90 days), 257,646 (365 days)
build-error: 0 (30 days)

色々と表示されますが、今回必要なのは下の方に表示された、

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

の部分だけです。ここで実際にPython3と一緒にインストールされた関連パッケージが/usr/local/opt/python@3.8/libexec/binにインストールされたことが分かりました。
なのでターミナルでこれをPATHに追加します。

$ echo "export PATH=/usr/local/opt/python@3.8/libexec/bin" >> ~/.zshrc
$ source .zshrc

ターミナルがbashの場合はこちら。

$ echo "export PATH=/usr/local/opt/python@3.8/libexec/bin" >> ~/.bash_profile
$ source .bash_profile

vimで直接プロファイル等を編集する場合は以下の内容を追加して下さい。

export PATH=/usr/local/opt/python@3.8/libexec/bin

pythonpipでそれぞれPython3が動きます。

$ python -V
Python 3.8.5

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

PATHを通す代わりにエイリアスを設定する方法でも動きます。PATHの方がいいとは思いますが。

$ echo "alias python=python3" >> .zshrc
$ echo "alias pip=pip3" >> .zshrc
$ source .zshrc

おまけ

今回インストールしたPython3をアンインストールするときは下記のコマンドで実行します。

$ brew uninstall python

あとがき

pyenvを使用するよりも手順としては手軽ですね。
ただPATHを通さなければいけませんし、そのPATHも見る限りではPython3のバージョンが変わる度に変動しそうなので、Python3を更新する度にPATHの再設定が必要になりそうな予感がします。

本格的に開発する前に試しに触って見るだけならこの方法でインストールしてもいいかもしれませんが、この方法でインストールしたPython3を開発に使用するのはやめたほうがいいと思います。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1