Make python3 as my default python on Mac

105,142

Solution 1

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

Solution 2

Before we make the changes, the default version of python in my system was python 2.7.17.

python --version

Python 2.7.17

To make python3 as default python by replacing python2 in Ubuntu.

  1. Open Terminal
  2. cd
  3. nano ~/.bashrc
  4. alias python=python3 (Add this line on top of .bashrc file)
  5. Press ctr+o (To save the file)
  6. Press Enter
  7. Press ctr+x (To exit the file)
  8. source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)

python --version

Python 3.7.5

Solution 3

Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.

If you are using zsh (the default on Mac OS) run the following from terminal:

echo 'alias python="python3"' >> ~/.zshrc

Solution 4

According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.

The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.

Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

Share:
105,142
Sambo Kim
Author by

Sambo Kim

By day : Ordinary Student figuring out shits on him By Night : Kicked ass boy in Jiujitsu

Updated on January 26, 2022

Comments

  • Sambo Kim
    Sambo Kim over 2 years

    What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx

    I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.

    The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

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

    I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.

    export PATH=/usr/local/bin:/usr/local/sbin:$PATH
    # Setting PATH for Python 3.6
    # The original version is saved in .profile.pysave
    export PATH=/usr/local/bin:/usr/local/sbin:$PATH
    

    And then I did brew install python, and was hoping to see python3 when I do python --version. But it just shows me python 2.7.10. I want my default python to be python3 not 2.7

    And I found a little clue from the website.

    Do I have a Python 3 installed?

    $ python --version
    Python 3.6.4
    

    If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/

    Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?

  • OneCricketeer
    OneCricketeer about 6 years
    I'm pretty sure Homebrew automatically updates the PATH. There's no reason to explicitly hardcode python libexec folder in there
  • avigil
    avigil about 6 years
    I thought they stopped doing this last year: github.com/Homebrew/homebrew-core/pull/14408
  • orome
    orome about 5 years
    Doing this and then updating pip (with pip3 install -U pip) causes subsequent runs of pip to error with pkg_resources.VersionConflict: (pip 19.1 (/usr/local/lib/python3.7/site-packages), Requirement.parse('pip==19.0.3')).
  • Pysis
    Pysis almost 5 years
    This is mostly what I wanted, even from another Q&A superuser.com/questions/324616/…, not wanting an alias or system default change, but Homebrew to manage it, possibly with an option. This is close but still requires customization of the alias. I'm also curious what further testing with pip reveals.
  • Doug Barbieri
    Doug Barbieri about 4 years
    I decided that this is the best option, as you said: "python3". I just have to remember to type as well: "pip3" when installing new modules. Hehe.
  • philippos
    philippos almost 4 years
    This solved the problem only for once. Then, once I closed the terminal are tried again, it reverted again to the old version.
  • ArtDev
    ArtDev almost 4 years
    Very well organized answer. :+1
  • Collins USHI
    Collins USHI almost 4 years
    Answer is straight to the point. It worked on macOS catalina 10.15.3
  • lexlab
    lexlab almost 4 years
    Perfect answer -
  • Abdurahman
    Abdurahman almost 4 years
    @philippos For mac add the path to /etc/paths to make it permanent stackoverflow.com/questions/22465332/… and this for Linux cyberciti.biz/faq/how-to-add-to-bash-path-permanently-on-lin‌​ux
  • nightrain
    nightrain over 3 years
    Although the version is changed to python3, but the default python is still python 2.7. Like when you open a new jupyter notebook, or install a new package, the active python is still 2.7.
  • Pravanjan
    Pravanjan over 3 years
    Very helpful answer in case ~/.bashrc not found. check for ~/.bash_profile or ~/.zshenv.
  • Teghan Nightengale
    Teghan Nightengale over 3 years
    Just a note that on Catalina, adding the path to /etc/paths did not cause python to resolve to python 3 for new terminal windows. Still looking for a solution...
  • harley
    harley over 3 years
    This should be marked as a correct answer!
  • BigDreamz
    BigDreamz almost 3 years
    this only works for terminal sessions, however if you close your terminal and reopen your terminal you have source bashrc again. Is there a permanent solution?
  • Lord Elrond
    Lord Elrond almost 3 years
    @BigDreamz I don't understand what you mean by "permanent" ?
  • BigDreamz
    BigDreamz almost 3 years
    never mind, it works for me now. I had to source zshrc
  • Jim O.
    Jim O. over 2 years
    Wonderful. Thank you so much.
  • Edward Brey
    Edward Brey about 2 years
    This answer still works for the the latest Homebrew formula that lists /usr/local/opt/[email protected]/libexec/bin in Caveats, since /usr/local/opt/python provides a suitable symlink.
  • Chris
    Chris about 2 years
    brilliant. Never stop being you.
  • DoPeT
    DoPeT about 2 years
    Perfect answer, thank you so much! Simple as that.
  • Sathish Sundar
    Sathish Sundar about 2 years
    Great simple and perfect answer