brew-installed Python not overriding system python
TL;DR Add the following to your .bash_profile
(or equivalent):
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Explanation
It seems python via homebrew is now handled differently (see https://docs.brew.sh/Homebrew-and-Python).
python3
points to Homebrew’s Python 3.x (if installed)python2
points to Homebrew’s Python 2.7.x (if installed)python
points to Homebrew’s Python 2.7.x (if installed) otherwise the macOS system Python. Check outbrew info python
if you wish to add
Homebrew’s 3.x python to your PATH.
Checking out brew info python
hints at what you need to do:
Unversioned symlinks
python
,python-config
,pip
etc. pointing to
python3
,python3-config
,pip3
etc., respectively, have been
installed into /usr/local/opt/python/libexec/bin
The hint being that you therefore have to add /usr/local/opt/python/libexec/bin
before /usr/bin
in your path (not /usr/local/bin
as stated in some sources e.g. https://docs.python-guide.org/starting/install3/osx/)
See also https://github.com/Homebrew/homebrew-core/issues/15746
Switching Python version installed by Homebrew
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.
How to default Python3.8 on my Mac using Homebrew?
Here is the solution:
If existing symlinks belong to python 3.7 you should unlink them:brew unlink python
Basically all you need to do:brew link --force python@3.8
OR force the link and overwrite all conflicting files:brew link --force --overwrite python@3.8
OR if needed list all files that would be deleted:brew link --overwrite --dry-run python@3.8
Thus you can switch to any python version available in the Homebrew repo.
Also check out this answer for pyenv usage
Make python3 as my default python on Mac
Probably the safest and easy way is to use brew and then just modify your PATH
:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin
:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH
is important, by putting first the /usr/local/opt/python/libexec/bin
will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
How to make Mac OS use the python installed by Homebrew
If you want to install Python 3 using Homebrew:
$ brew install python3
==> Downloading http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.3.0.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.3.0 --enable-ipv6 --datarootdir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0/share/python3
==> Downloading https://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz
Already downloaded: /Library/Caches/Homebrew/distribute-0.6.35.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Downloading https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/pip-1.3.1.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Caveats
Homebrew's Python3 framework
/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework
Distribute and Pip have been installed. To update them
pip3 install --upgrade distribute
pip3 install --upgrade pip
To symlink "Idle 3" and the "Python Launcher 3" to ~/Applications
`brew linkapps`
You can install Python packages with
`pip3 install <your_favorite_package>`
They will install into the site-package directory
/usr/local/lib/python3.3/site-packages
Executable python scripts will be put in:
/usr/local/share/python3
so you may want to put "/usr/local/share/python3" in your PATH, too.
See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
Once installed update your system PATH
variable, add the next line to ~/.bash_profile
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
And then:
$ source ~/.bash_profile
Now launch Python:
$ python3
Python 3.3.0 (default, Mar 26 2013, 10:01:40)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
You can check python3 path:
$ which python3
/usr/local/bin/python3
Find path of Python installed by Homebrew
Use brew info <packagename>
it's probably in one of (and referenced in both)
/usr/local/Cellar/python@3.8/3.8.6_2
/usr/local/opt/python@3.8/libexec/bin
See also Apple SE Where can I find the installed package path via brew
If it's not there, it's plausible the version detection is wrong and only searches for the first two version digits (as conflicting versions may clobber each other). In this case, follow the warning and reinstall the specific version you want.
Related Topics
Python: Http Post a Large File with Streaming
Why Use Abstract Base Classes in Python
Unnamed Python Objects Have the Same Id
How to Plot Nan Values as a Special Color with Imshow in Matplotlib
Matplotlib: How to Plot Images Instead of Points
How to "Properly" Print a List
Sorting Python List Based on the Length of the String
Set Markers for Individual Points on a Line in Matplotlib
How to Set the Figure Title and Axes Labels Font Size in Matplotlib
How to Get Flask to Run on Port 80
List of Tables, Db Schema, Dump etc Using the Python SQLite3 API
Correct Way to Define Class Variables in Python
Getting Rid of Console Output When Freezing Python Programs Using Pyinstaller
How to Filter Pandas Dataframes by Multiple Columns