52

I have Python 3.8 and 3.9 installed via Homebrew:

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

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/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?

Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • Check out this: https://stackoverflow.com/questions/18671253/how-can-i-use-homebrew-to-install-both-python-2-and-3-on-mac – Tom Oct 14 '20 at 23:28
  • @Tom Are you sure it applies here? Python 2 and Python 3 are separate packages. – Robo Robok Oct 14 '20 at 23:44
  • For me Brew was only addressing `python3` and would not add a `python` link. I had to go in and cut up my `.bashrc` and `.profile` to edit my `$PATH` which was getting mangled by some inserts, presumably from some earlier installs, and then add a `python` symlink to the `python3` that I wanted, alongside the `python3` symlink in `/opt/homebrew/bin`. – NeilG May 30 '22 at 02:10

3 Answers3

92

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 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.

Jean-Pierre Matsumoto
  • 1,917
  • 1
  • 18
  • 26
  • 1
    Once followed the steps you described, it's ok to use `brew uninstall python@3.8` as it will be not have anymore dependencies? – NZisKool Apr 14 '21 at 08:57
  • Works for me without `--force` and console re-openning, at least `python3 --version` shows linked python version. – mrgloom Aug 02 '21 at 15:21
  • Also here is example where I have some problems after unlink / link https://stackoverflow.com/questions/68634761/env-python3-9-no-such-file-or-directory/68729295#68729295 – mrgloom Aug 10 '21 at 15:11
  • The third command resulted in this error message: `Error: Could not symlink bin/2to3 Target /usr/local/bin/2to3 already exists. You may want to remove it: rm '/usr/local/bin/2to3' To force the link and overwrite all conflicting files: brew link --overwrite python@3.9` I did that, and it worked – rturquier Sep 28 '21 at 16:06
  • It does not work for modern days... please disregard (I wish there is a modern day downvote button!) – Eric Chen Dec 31 '22 at 06:54
6

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.

Seth
  • 2,214
  • 1
  • 7
  • 21
  • 2
    Why wouldn't I want to install `pyenv` through Homebrew? – Robo Robok Oct 15 '20 at 07:56
  • The script above does install through Homebrew *and* sets it up to launch `pyenv` on bootup. @RoboRobok – Seth Oct 15 '20 at 14:56
  • 1
    How come? There's `brew install pyenv` for that :D – Robo Robok Oct 15 '20 at 15:18
  • Yes, but it also modifies the on-boot script, which `brew install` doesn't do. It just is less hassle. @RoboRobok – Seth Oct 15 '20 at 16:31
  • 10
    This doesn't work for me. After installing with "brew install pyenv" running "pyenv versions" only lists "system" despite me having 3.8, 3.9 and 3.10 installed via Homebrew too. – markshep Apr 01 '22 at 14:20
  • I previously installed python with Homebrew and had to remove it via "brew uninstall python@3.9". After that I installed a previous version via "pyenv install 3.8.6" and "pyenv versions" returned the correct version under "/Users/${USER}/.pyenv/version". – Hahnemann Jul 05 '22 at 03:28
4

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.

Navid Khan
  • 979
  • 11
  • 24
Everton Costa
  • 570
  • 5
  • 16