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:
- You can unlink the system default's python3 and relink python3 to the homebrew version (see kraspa11's answer)
- 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/python@3.11 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).
export PATH="/opt/homebrew/opt/python@3.11/libexec/bin:$PATH"– David Nov 19 '22 at 20:59python3.11topython3vialn. But I do not encourage that since this will overwrite Python 3.10 and you may have broken dependancies and mess upbrew. – AlessioX Dec 26 '22 at 08:51brew --prefixworks – Ezekiel Mar 10 '23 at 18:20/usr/local/opt/python@3.11/libexec/binwhich is the crucial part. That dir will not be onPATH-/opt/homebrew/binwill be./opt/homebrew/binwill contain an unversioned symlinkpython3, that points to some Homebrew../Cellar/..dir. For the OP, that was the cellar dir of Python3.10, and that's why3.10was invoked on a commandpython3. – Carl Dec 17 '23 at 00:38brew --prefix. That command will, with some caveats, setHOMEBREW_PREFIX, but that variable won't be available untilbrew --prefixis first invoked. (I believe one reason forbrewto automatically set the variable is so repeated use can refer to the variable rather than doingbrew --prefix- if you need many of them in your startup files, it will accumulate some execution time sincebrew --prefixnecessarily forks a new shell.) – Carl Dec 17 '23 at 00:46