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

676,349

Solution 1

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.

Solution 2

You can solve it by symbolic link.

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python

Solution 3

Open ~/.bash_profile file.

vi ~/.bash_profile

Then put the alias as follows:

alias python='python3'

Now save the file and then run the ~/.bash_profile file.

source ~/.bash_profile

Congratulation !!! Now, you can use python3 by typing python.

python --version

Python 3.7.3

Solution 4

I encountered this issue as well, so I thought I should post an updated answer. Please note that this will only apply to a Mac-based setup (I haven't tried it with Windows or any flavor of Linux). The simplest way to get this working is to install Python via Brew. If you don't have brew installed, you will need to do that first. Once installed, do the following in at the terminal:

brew install python

This will install Python 3. After it's installed, run this:

ls -l /usr/local/bin/python*

You will see all of the links created by brew to its Python install. It will look something like this:

lrwxr-xr-x  1 username  admin  36 Oct  1 13:35 /usr/local/bin/python3@ -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x  1 username  admin  43 Oct  1 13:35 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x  1 username  admin  38 Oct  1 13:35 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x  1 username  admin  45 Oct  1 13:35 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x  1 username  admin  39 Oct  1 13:35 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x  1 username  admin  46 Oct  1 13:35 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7m-config

The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:

ln -s -f /usr/local/bin/python3 /usr/local/bin/python

You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:

exec $SHELL -l

You're all set now. Now, you can do:

which python

and it should show:

/usr/local/bin/python

All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:

python --version

Solution 5

Go to terminal type:

alias python=python3.x

This will setup default python as python3.x

Share:
676,349
Marcus
Author by

Marcus

Hack. Hack. Hack.

Updated on December 06, 2021

Comments

  • Marcus
    Marcus over 2 years

    I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.

    Currently:

    $ python
        version 2.7.5
    $ python3.3
        version 3.3
    

    How do I set it so that every time I run $ python it opens 3.3?

  • UnsettlingTrend
    UnsettlingTrend almost 8 years
    Should this not be put in ~/.bash_profile instead of ~/.bash_aliases?
  • Haymaker87
    Haymaker87 almost 8 years
    Putting alias python=python3 and then running python in my terminal on osx el capitan didn't work for me. Tried saving it both ~/.bash_aliases and ~/.bash_profile.
  • Wei Lu
    Wei Lu almost 8 years
    @Haymaker87 run source ~/.bash_profile after edit ~/.bash_profile file.
  • tread
    tread almost 7 years
    You can do the same for pip: alias pip='pip3.6'
  • Santosh Kumar
    Santosh Kumar almost 7 years
    @surfer190 that will cause problem when running with sudo, isn't it?
  • Edison
    Edison over 6 years
    @SantoshKumar If you're using virtualenv/virtualenvwrapper for activation do you still need to use #!/usr/bin/env python3 or any other shebang + path at the top of python files?
  • Alper
    Alper over 6 years
    Is this everything that is required for me to switch to python3 in my shell and applications?
  • Santosh Kumar
    Santosh Kumar over 6 years
    @Alper this was for shell only. The tip part is for developers, but don't change if either one of them is already there. It might break.
  • Chad Befus
    Chad Befus over 6 years
    This is the correct answer (aliases are nice but only accessible by bash, which limits where you can call from). However, I would use unlink instead of rm to remove symlinks (if you accidentally add a trailing slash on rm you might have some bad results). Alternatively, you could do ln -s -f ... which should overwrite the current symlink.
  • Shin Kim
    Shin Kim over 6 years
    @ChadBefus Thank you for your reply. I agree with your opinion. unlink is safer than rm.
  • Anton Tarasenko
    Anton Tarasenko over 6 years
    Does it have any consequences for scripts that expect python to be python2.7?
  • 0x12
    0x12 over 6 years
    It does not happen
  • Jazzmine
    Jazzmine over 6 years
    Can you do the reverse to reestablish python2x as the default version? So if you have linked python with python3, I'm suggesting then you can unlink python3 and ln -s python2.
  • user924
    user924 almost 6 years
    it does nothing
  • MarksCode
    MarksCode almost 6 years
    macOS: unlink: /usr/bin/python: Operation not permitted
  • Henry Tseng
    Henry Tseng almost 6 years
    Shouldn't cause any issues with sudo but will cause issues if you don't source your alias. This is more of a workaround.
  • EliadL
    EliadL over 5 years
    @MarksCode try to just run the second original line (i.e. ln -s... with /local/).
  • Taylor A. Leach
    Taylor A. Leach over 5 years
    It won't take until you close your terminal window after the change to the .bash_profile are made.
  • kidkkr
    kidkkr about 5 years
    If your app requires python3 for running, you should edit symlinks not alias.
  • AlexeyGy
    AlexeyGy almost 5 years
    you should set the pip alias not to pip3.6 specifically, instead set it to pip3: ``alias pip='pip3'
  • Ani
    Ani almost 5 years
    adding -> alias python='python3' in ~/.bash_profile file worked for me.
  • jjwdesign
    jjwdesign almost 5 years
    For Mac OS users, you should use the brew commands to make the symlinks automatically. For example: brew unlink python, brew switch python 3.x.x_x (or 2.x.x), brew python link python (or python@2). Note, brew installs python 3 as "python3".
  • Ganesh M S
    Ganesh M S over 4 years
    This did not work for me. After following this still python2 is invoked by default.
  • Dani
    Dani over 4 years
    Would it still break things now in Catalina?
  • Santosh Kumar
    Santosh Kumar over 4 years
    Nope, its bash thing. Nothing to do with the operating system.
  • Santosh Kumar
    Santosh Kumar over 4 years
    Doesn't this risks the application dependent on python2? Definitely it will go away when Python 3 is mainstream, but still. Correct me if I'm missing something.
  • Vlad K.
    Vlad K. over 4 years
    The real major problem with recent Mac OSs is it doesn't give a $#%^ about UNIX commands and notion of root: >> sudo unlink /usr/bin/python Password: unlink: /usr/bin/python: Operation not permitted >> sudo -su root >> whoami root >> unlink /usr/bin/python unlink: /usr/bin/python: Operation not permitted
  • SnellyBigoda
    SnellyBigoda over 4 years
    If you found like I did that the same issue applies for pip (i.e. the pip --version is still 2.7.x) then you'll need to do the same steps for pip3. vi ~/.bash_profile, alias pip='pip3', source ~/.bash_profile, pip --version
  • Discant
    Discant about 4 years
    This worked for me but using "alias python=python3"
  • Ashwin R
    Ashwin R about 4 years
    This will temporarily set python version for that terminal instance.
  • Ricky Aguilar
    Ricky Aguilar about 4 years
    Do I need to source ~/.bash_profile everytime i close and open my terminal? It says command not found after I re-opened my terminal
  • Ricky Aguilar
    Ricky Aguilar about 4 years
    Do I really need to source ~/.bash_profile everytime I re-opened my terminal? It reverts to python2 once I close and open the terminal again.
  • Nepo Znat
    Nepo Znat almost 4 years
    Works great (Reference)
  • TimewiseGamgee
    TimewiseGamgee almost 4 years
    This feels like the cleaner approach. I set mine to python 3.5 and it worked.
  • Craig Conover
    Craig Conover over 3 years
    Not sure why you got a down vote. This worked for me! Thanks for posing this. I upvoted you to cancel that down vote ;)
  • Pei
    Pei over 3 years
    I had to close terminal and open a new one to get it working in macOS
  • Robin
    Robin over 3 years
    Whilst this is a quick fix there are some potential issues with this solution, especially because you still depend on the python version Apple ships by default. This article explains them in depth as well as alternative solutions that avoid these problems.
  • cs_pupil
    cs_pupil over 3 years
    On Catalina (10.15.17), I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.
  • cs_pupil
    cs_pupil over 3 years
    "You will have to reload your current terminal shell for it to use the new symlink in that shell." Thanks, that was the piece I was missing from all the answers so far.
  • MIike Eps
    MIike Eps almost 3 years
    this does not work for me
  • Nikhil Das Nomula
    Nikhil Das Nomula almost 3 years
    I had to do this in my case ln -s -f /usr/local/Cellar/[email protected]/3.8.11/bin /usr/local/bin/python3
  • iDevSpread
    iDevSpread over 2 years
    It worked for me,tkx
  • Xitcod13
    Xitcod13 over 2 years
    Error: Unknown command: switch
  • sohammondal
    sohammondal over 2 years
    this also worked for me. A few things I would like to add - 1. I am using Mac M1, so I had to run arch -arm64 brew install python & 2. pip wasn't working for me, so I had to run sudo ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip and then pip worked.
  • sknight
    sknight over 2 years
    @sohammondal Thanks you. I appreciate your input.
  • Mustafa
    Mustafa about 2 years
    ln: /usr/bin/python: Operation not permitted
  • CN_Cabbage
    CN_Cabbage about 2 years
    @RickyAguilar Me either. My current conda base is 3.8, I want to change the default pythton to 3.10. I have changed the ~/.bash_profile, by commenting out the existing one and adding export PATH="/Applications/anaconda3/envs/py3.10/bin:$PATH" it works after source. However, once I quite the terminal and reopen, it turns out the base is still py3.8. and i need to source again. Which is unresonable. Since I can conda activate py3.10. Have u find another way to tackle this?
  • L. Heinrichs
    L. Heinrichs almost 2 years
    For me it looked like this: brew link [email protected]. Thanks for hints!
  • Rohan Kumara
    Rohan Kumara almost 2 years
    if you already installed visual studio code it will be easy to use. code ~/.bash_profile instead of using. vi ~/.bash_profile.
  • Rai Rz
    Rai Rz almost 2 years
    Just this code worked for my Mac, thanks