44

I like new software and I noticed that 3.11 is available in brew, so I have installed it:

brew install [email protected]

I can get a repl by running python3.11, but if I run python3 I still get python 3.10

I have unlinked [email protected] and then tried linking [email protected] and it tells me it is already linked (unlinking 3.11 first then relinking didn't help) and then when I try to run python3 it gives me python3.9, which I believe is installed from brew install python3 (If I try to remove python3.9 it tells me some software is dependant on it and I shouldn't do it...)

Anyway, is there any way to link the default brew python3 to use python3.11? I tried brew switch but I guess that command is deprecated...

Or is there some mechanism in brew that stops new versions from being used as a default?

3 Answers 3

40

Note: This applies to any versioned Python installation with Homebrew

After installing Python 3.11, you are given the location of the bin folder with Python 3.11:

==> Caveats
Python has been installed as
  /usr/local/bin/python3.11

Unversioned and major-versioned symlinks `python`, `python3`, `python-config`, `python3-config`, `pip`, `pip3`, etc. pointing to
`python3.11`, `python3.11-config`, `pip3.11` etc., respectively, have been installed into
  /usr/local/opt/[email protected]/libexec/bin

You can add this folder to be first in your PATH and python or python3 will pick up Python 3.11 (since those are in the folder):


So: add the following line to ~/.zshrc:

export PATH="$(brew --prefix)/opt/[email protected]/libexec/bin:$PATH"

brew --prefix ensures that the correct path is used, regardless of where your brew install resides.

8
  • 1
    Thanks, I am aware that you can update your PATH in this way, but I want to know how to use brew to set 3.11 as my default, like I can with 3.9/3.10
    – Rumbles
    Commented Nov 10, 2022 at 13:16
  • 12
    You cannot until the default Python is updated to 3.11 which they don’t do until things are compatible.
    – Ezekiel
    Commented Nov 10, 2022 at 15:27
  • 7
    For Apple silicon Macs, the export would need to be export PATH="/opt/homebrew/opt/[email protected]/libexec/bin:$PATH"
    – David
    Commented Nov 19, 2022 at 20:59
  • 1
    @sorin I fixed your attempted edit, HOMEBREW_PREFIX isn't a variable that exists but brew --prefix works
    – Ezekiel
    Commented Mar 10, 2023 at 18:20
  • 1
    @Ezekiel @sorin: it should indeed be brew --prefix. That command will, with some caveats, set HOMEBREW_PREFIX, but that variable won't be available until brew --prefix is first invoked. (I believe one reason for brew to automatically set the variable is so repeated use can refer to the variable rather than doing brew --prefix - if you need many of them in your startup files, it will accumulate some execution time since brew --prefix necessarily forks a new shell.)
    – Carl
    Commented Dec 17, 2023 at 0:46
15

On macOS Ventura & Sonoma on Apple Silicon

To install homebrew's version of python3:

brew install python3

This installs brew's version of python3.x but does not override the the link to macOS's version of python3 when you use the python3 command. You can confirm which python3 is being used with the which python3 command. Now, you have two primary options here:

  1. You can unlink the system default's python3 and relink python3 to the homebrew version (see kraspa11's answer)
  2. You can simply add an alias for python3 in your ~/.zshrc file which can point to brew's version instead (I prefer this because it results in fewer edits to system defaults).

If you prefer option #2:

Open up ~/.zshrc with nano ~/.zshrc or your desired editor. BTW, if you want to see "hidden" files/folders in your /Users/[username] folder where .zshrc resides, open your user folder in finder and press ⇧ Shift + ⌘ Command + . (period). This will show/hide all hidden files and folders in your Finder.

At the end of the .zshrc file, add the following lines:

# ensure "python3" command uses homebrew's version of python3
alias python3=/opt/homebrew/bin/python3
# OPTIONAL: ensure "python" command uses homebrew's version of python3
alias python=/opt/homebrew/bin/python3

I use /opt/homebrew/bin/python3 instead of /opt/homebrew/opt/[email protected] so if/when you update your version from 3.11 to 3.12 and so on, your aliases should always work (until version 4).

Now, save and close your edited .zshrc file. To refresh your terminal to use the new alias(es), enter:

source ~/.zshrc

To ensure that your commands are pointing to the brew version of python3:

which python3 should respond: python3: aliased to /opt/homebrew/bin/python3

and

which python should respond: python: aliased to /opt/homebrew/bin/python3

NOTE: I have only tested all of the above on Apple silicon macOS Sonoma (which is in developer beta, but should be functionally the same as Ventura).

5
  • 1
    Those using Oh My Zsh might prefer to make a file something like python.zsh in your $ZSH/custom directory, and put aliases similar to the above for pip, python-config and whatnot.
    – Jerry
    Commented Nov 15, 2023 at 21:03
  • 1
    This answer is slightly confused: there is system Python, managed by macOS, at /usr/bin/python3 (a binary). Then there is homebrew Python, at /opt/homebrew/bin/python3 (a symlink to a specific homebrew-installed Python version). What version /opt/homebrew/bin/python3 points to is up to homebrew; they choose a "latest stable release" and you can have later Python versions installed with Homebrew - that still won't be linked to from /opt/homebrew/bin/python3, and there are no brew link etc. commands that can force it to a later version than what homebrew has chosen.
    – Carl
    Commented Dec 17, 2023 at 0:56
  • 1
    If you want a command python3 to invoke /opt/homebrew/bin/python3, you can make sure the latter has precedence than (comes before) system Python in PATH. You could also set an alias or create a function python3. If you want python3 to invoke a later Python version than what homebrew links /opt/homebrew/bin/python3 to, you can modify PATH to have the desired .../bin/ dir with higher precedence, or set an alias or create a function.
    – Carl
    Commented Dec 17, 2023 at 1:02
  • @carl I agree with everything you said. There's a lot of ways to achieve the same desired result and my way may not be the most optimized or direct way. Also, Jerry has a point...using the custom folder of zsh is probably the "cleaner" more modular way of customizing your zsh aliases etc...but I just wanted to lay out a quick and easy way of getting this to work for anyone that just wanted to start using Python3 with their (at the time, Beta version of MacOS).
    – Doomd
    Commented Jan 30 at 5:46
  • you can use $(brew --prefix [email protected])/libexec/bin
    – Dawoodjee
    Commented Feb 29 at 15:09
5

List your python binary symlinks in /opt/homebrew/bin by running:

ls -la /opt/homebrew/bin | grep "python"

In your case python3 should be symlinked to python3.10. In my case, after running brew unlink python3, all python3.11(!) symlinks were removed from the directory. After that running python3 --version still showed 3.10.

After running brew unlink [email protected] all of my python3.10 symlinks were removed also, so that no python symlinks existed anymore in /opt/homebrew/bin.

Finally I was able to link python3.11 to /opt/homebrew/bin/python3 by running brew link python.

Hope this helps!

3
  • 1
    This didn't work for me (Ventura 13.3.1, python 3.11) 😐. Commented May 24, 2023 at 10:48
  • @RobertAtkins please see my answer: apple.stackexchange.com/a/461063/31585
    – Doomd
    Commented Jun 14, 2023 at 23:15
  • This solution was useful for my particular use case on Mac OS 14.2.1 x86-64bit Commented Jan 4 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .