31

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
  • 18,202
  • 14
  • 57
  • 101
  • 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

2 Answers2

61

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,363
  • 1
  • 11
  • 23
  • Thanks for answering. When I do `brew link python@3.9`, I get the following message: `python@3.9 is keg-only and must be linked with --force` – Robo Robok Oct 19 '20 at 13:21
  • You're right. I've forgot `--force`. I edit my answer. – Jean-Pierre Matsumoto Oct 19 '20 at 13:31
  • I recommend closing and re-opening the terminal too after running this. – moltenform Jan 06 '21 at 21:33
  • @moltenform ok, I added your tips. Thanks – Jean-Pierre Matsumoto Jan 08 '21 at 15:16
  • 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
0

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,049
  • 1
  • 5
  • 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
  • 1
    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