How to Default Python3.8 on My MAC Using Homebrew

How to default Python3.8 on my Mac using Homebrew?

Ok, thanks to @gromgit from Homebrew community discussion (https://discourse.brew.sh/t/how-to-default-python-3-8-on-my-mac-using-homebrew/7050)

Here is the solution:

$ brew info python@3.8
python@3.8: stable 3.8.1 (bottled) [keg-only]
...
==> Caveats
Python has been installed as
/usr/local/opt/python@3.8/bin/python3
...
python@3.8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have python@3.8 first in your PATH run:
echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.bash_profile

For compilers to find python@3.8 you may need to set:
export LDFLAGS="-L/usr/local/opt/python@3.8/lib"

For pkg-config to find python@3.8 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"

I will stick to python (v3.7.6) at this time and wait for seamless upgrade of v3.8.1 in the future releases.

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.

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 set Python3 as a default python version on Mac?

I think you can run the following commands :

rm /usr/local/bin/python
ln -s /usr/local/bin/python3.8 /usr/local/bin/python

And in your ~/.zshrc or ~/.bashrc, put

export PATH=/usr/local/bin:$PATH

Then start a new terminal to test

echo $PATH

to make sure /usr/local/bin is before /usr/bin

Python official newest version, Homebrew version and installed version differ on macOS

After some research I found out the following reasons:

  • It is possible to upgrade to Python 3.8.2 via Homebrew, but it isn't listed as supplying python due to some complications. See status here. Until this issue is done, 3.7.6_1 is the newest version you can upgrade to (as Homebrew kept telling me).

  • When I ran python3 --version in terminal, it returned Python 3.7.3 because I had installed this version myself at some point from the official website, without using Homebrew. Upgrading python to 3.7.6 via Homebrew had no effect on this dominating installation of python. I got rid of the manually installed version using this useful answer, after which I reinstalled python properly using Homebrew just to be on the safe side and now I finally have:

    Nannas-Computer:some_folder username $ python3 --version
    Python 3.7.6

How to set Python's default version to 3.x on OS X?

Changing the default python executable's version system-wide could break some applications that depend on python2.

However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put
alias python='python3'
in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:

[ -e ~/.profile ] && . ~/.profile

This way, your alias will work across shells.

With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.

If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:

alias 2='python2'
alias 3='python3'

Tip: For scripts, instead of using a shebang like:

#!/usr/bin/env python

use:

#!/usr/bin/env python3

This way, the system will use python3 for running python executables.



Related Topics



Leave a reply



Submit