つかぱい.com

どうせなら好きなことを書いていたい

HomebrewでPythonを2系から3系に切り替える

Python2のサポートが終了したので、3系をメインに切り替えしようと思った。

2系が必要になったら後で切り替えられるようにしたい

設定方法

brew install python3でpython3をインストール

 ~  brew install python3                                                                                    2065ms  Fri Jan 31 07:51:57 2020
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/python-3.7.6_1.mojave.bottle.tar.gz
Already downloaded: /Users/pcuser/Library/Caches/Homebrew/downloads/814567972a37dafb5f922cc993d13dd59c468da5113385e93d1bbfceccaba83f--python-3.7.6_1.mojave.bottle.tar.gz
==> Pouring python-3.7.6_1.mojave.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink Frameworks/Python.framework/Headers
Target /usr/local/Frameworks/Python.framework/Headers
is a symlink belonging to python@2. You can unlink it:
  brew unlink python@2

To force the link and overwrite all conflicting files:
  brew link --overwrite python

To list all files that would be deleted:
  brew link --overwrite --dry-run python

Possible conflicting files are:
/usr/local/Frameworks/Python.framework/Headers -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/Current -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/Current
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pytho
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pytho
==> /usr/local/Cellar/python/3.7.6_1/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/pytho
==> 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/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  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
==> Summary
🍺  /usr/local/Cellar/python/3.7.6_1: 3,977 files, 60.8MB

インストールしたらPython2のライブラリのパスとコンフリクトしていると言われる

もしPython2を普段使わない人はここでUnlinkしてしまうのがいいかかも

ログにある通りUnlinkサブコマンドがあるので--dry-runオプションをつけて実行

brew link --overwrite --dry-run python

!  ~  brew link --overwrite --dry-run python                                                               26.7s  Fri Jan 31 07:57:52 2020
Would remove:
/usr/local/Frameworks/Python.framework/Headers -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/Current -> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/Current

こんなの出てきたけど別に問題ないので--dry-runオプションを外して実行

brew link --overwrite python

 ~  brew link --overwrite python                                                                            1526ms  Fri Jan 31 07:57:54 2020
Linking /usr/local/Cellar/python/3.7.6_1... 24 symlinks created

リンクを更新した。

ただこのままだとバージョンが2系のままになってしまっているので

alias python=python3エイリアスを設定する。

おまけ

pipもデフォルトはPython2なのでエイリアスを設定しておくと変なエラーを吐かなくていいかも

開発/Python