如何使用 Homebrew 在我的 Mac 上默认设置 Python3.8?

How to default Python3.8 on my Mac using Homebrew?

我已经将我的 python 3 更新到最新版本 3.8:

brew search python
==> Formulae
app-engine-python   gst-python          python ✔            python@3.8 ✔
boost-python        ipython             python-markdown     wxpython
boost-python3       micropython         python-yq

==> Casks
awips-python               kk7ds-python-runtime       mysql-connector-python

但是当我在 mac 上检查 python3 版本时,它仍然显示 3.7:

python3 --version

Python 3.7.6

如何使用 Homebrew 将 python3 默认为最新的 3.8 版本?

编辑: 当我尝试使用 brew switch 时,它告诉我我只安装了 python 3.7.6,但是最后一个 brew upgrade 我很确定 python3.8.1 是用 Homebrew

brew switch python 3.8.1
python does not have a version "3.8.1" in the Cellar.
python's installed versions: 3.7.6_1

您可能需要将 python 3.8.1 添加到 ~/.bash_profile 中的 PATH 并将其放在第一位,以便它覆盖以前的安装。首先找出 python 3.8.1 的安装位置,然后像这样将其添加到您的路径中:

导出 PATH="/PATH_TO_PYTHON/:${PATH}"

好的,感谢 Homebrew 社区讨论中的@gromgit (https://discourse.brew.sh/t/how-to-default-python-3-8-on-my-mac-using-homebrew/7050)

解决方法如下:

$ brew info python@3.8
python@3.8: stable 3.8.1 (bottled) [keg-only]
...
==> Caveats
Python has been installed as
  /usr/local/opt/python@3.8/bin/python3
...
python@3.8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have python@3.8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.bash_profile

For compilers to find python@3.8 you may need to set:
  export LDFLAGS="-L/usr/local/opt/python@3.8/lib"

For pkg-config to find python@3.8 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"

这次我会坚持使用python(v3.7.6),等待未来版本无缝升级v3.8.1。

解决方法如下:

如果现有的 symlinks 属于 python 3.7 你应该取消 link 它们:
brew unlink python

基本上您需要做的就是:
brew link --force python@3.8

或强制 link 并覆盖所有冲突文件:
brew link --force --overwrite python@3.8

或者如果需要列出所有将被删除的文件:
brew link --overwrite --dry-run python@3.8

因此您可以切换到 Homebrew 存储库中可用的任何 python 版本。

另请查看 pyenv 用法的回答

我有一家公司 Mac,Python 2.7 预装到 运行 旧软件。

$ brew install pyenv (successful)
$ pyenv install 3.9.2 (successful)
$ python --version
Python 2.7
$ pyenv global 3.9.2
$ python --version
Python 2.7 

它仍然显示 Python 2.7,所以,我执行了以下命令

pyenv init
eval "$(pyenv init -)"

$ python --version
Python 3.9.2

更多详情: Link