简体   繁体   English

切换 Homebrew 安装的 Python 版本

[英]Switching Python version installed by Homebrew

I have Python 3.8 and 3.9 installed via Homebrew:我通过 Homebrew 安装了 Python 3.8 和 3.9:

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

I want to use Python 3.9 as my default one with python3 command.我想使用 Python 3.9 作为python3命令的默认值。 I tried the following:我尝试了以下方法:

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

I tried to uninstall Python and reinstall it, but it's used by other packages:我试图卸载 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

How can I use Python 3.9?如何使用 Python 3.9?

There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9 .有一个 Homebrew 已知问题与并行安装 Python 3.8 / 3.9 相关 To workaround, following commands should work for you:要解决此问题,以下命令应该适合您:

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

Re-opening your terminal or execute command rehash can be required to take account the change.可能需要重新打开终端或执行命令rehash以考虑更改。

Use pyenv .使用pyenv It's a software that lets you switch between any and all Python versions installed on your system.它是一个软件,可让您在系统上安装的任何和所有 Python 版本之间切换。 To install pyenv , use the following code in the command-line:要安装pyenv ,请在命令行中使用以下代码:

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

Then find the name of the python version you want to switch to with this:然后找到要切换到的python版本的名称:

pyenv versions

And select it with this:并选择它:

pyenv global <version-name>

In your case, it's most likely named 3.9.0 .在您的情况下,它很可能被命名为3.9.0

Updated MacOs Monterrey更新了 MacOs Monterrey

For who are facing this problem add the pyenv path to your ~/.zshrc shell file.对于面临此问题的人,请将 pyenv 路径添加到您的~/.zshrc .zshrc shell 文件中。

export PATH="/Users/username/.pyenv/shims:${PATH}
eval "$(pyenv init --path)

Run in the terminal:在终端中运行:

source ~/.zshrc

Check it out:一探究竟:

python3 --version

Font from the issue on GitHub .来自GitHub问题的字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM