Switching Python version installed by Homebrew

Question:

I have Python 3.8 and 3.9 installed via Homebrew:

~ brew list | grep python
[email protected]
[email protected]

I want to use Python 3.9 as my default one with python3 command. 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:

~ brew uninstall python
Error: Refusing to uninstall /usr/local/Cellar/[email protected]/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?

Asked By: Robo Robok

||

Answers:

Use pyenv. It’s a software that lets you switch between any and all Python versions installed on your system. To install pyenv, use the following code in the command-line:

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

Then find the name of the python version you want to switch to with this:

pyenv versions

And select it with this:

pyenv global <version-name>

In your case, it’s most likely named 3.9.0.

Answered By: Seth

There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9. To workaround, following commands should work for you:

brew unlink [email protected]
brew unlink [email protected]
brew link --force [email protected]

Re-opening your terminal or execute command rehash can be required to take account the change.

Updated MacOs Monterrey

For who are facing this problem add the pyenv path to your ~/.zshrc shell file.

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

Run in the terminal:

source ~/.zshrc

Check it out:

python3 --version

From the issue on GitHub.

Answered By: Everton Costa
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.