切换 Homebrew 安装的 Python 版本

Switching Python version installed by Homebrew

我通过 Homebrew 安装了 Python 3.8 和 3.9:

~ brew list | grep python
python@3.8
python@3.9

我想使用 Python 3.9 作为带有 python3 命令的默认版本。我尝试了以下方法:

~ brew switch python 3.9
Error: python does not have a version "3.9" in the Cellar.
python's installed versions: 3.8.6

我尝试卸载 Python 并重新安装它,但它被其他软件包使用:

~ brew uninstall python
Error: Refusing to uninstall /usr/local/Cellar/python@3.8/3.8.6
because it is required by glib and php, which are currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies python

如何使用 Python 3.9?

使用pyenv。它是一种软件,可让您在系统上安装的任何和所有 Python 版本之间切换。要安装 pyenv,请在 command-line 中使用以下代码:

curl https://pyenv.run | bash
exec $SHELL

然后找到您要切换到的 python 版本的名称:

pyenv versions

和select它与此:

pyenv global <version-name>

在您的例子中,它很可能被命名为 3.9.0

存在与 side by side install of Python 3.8 / 3.9 相关的 Homebrew 已知问题。要解决此问题,请使用以下命令:

brew unlink python@3.9
brew unlink python@3.8
brew link --force python@3.9

Re-opening 您的终端或执行命令 rehash 可能需要考虑更改。