How to set Python's default version to 3.x on OS X?
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
Comments
-
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 almost 8 yearsShould this not be put in ~/.bash_profile instead of ~/.bash_aliases?
-
Haymaker87 almost 8 yearsPutting
alias python=python3
and then runningpython
in my terminal on osx el capitan didn't work for me. Tried saving it both ~/.bash_aliases and ~/.bash_profile. -
Wei Lu almost 8 years@Haymaker87 run
source ~/.bash_profile
after edit~/.bash_profile
file. -
tread almost 7 yearsYou can do the same for pip:
alias pip='pip3.6'
-
Santosh Kumar almost 7 years@surfer190 that will cause problem when running with sudo, isn't it?
-
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 over 6 yearsIs this everything that is required for me to switch to python3 in my shell and applications?
-
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 over 6 yearsThis 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 ofrm
to remove symlinks (if you accidentally add a trailing slash onrm
you might have some bad results). Alternatively, you could doln -s -f ...
which should overwrite the current symlink. -
Shin Kim over 6 years@ChadBefus Thank you for your reply. I agree with your opinion. unlink is safer than rm.
-
Anton Tarasenko over 6 yearsDoes it have any consequences for scripts that expect
python
to bepython2.7
? -
0x12 over 6 yearsIt does not happen
-
Jazzmine over 6 yearsCan 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 almost 6 yearsit does nothing
-
MarksCode almost 6 yearsmacOS:
unlink: /usr/bin/python: Operation not permitted
-
Henry Tseng almost 6 yearsShouldn't cause any issues with sudo but will cause issues if you don't source your alias. This is more of a workaround.
-
EliadL over 5 years@MarksCode try to just run the second original line (i.e.
ln -s...
with/local/
). -
Taylor A. Leach over 5 yearsIt won't take until you close your terminal window after the change to the .bash_profile are made.
-
kidkkr about 5 yearsIf your app requires python3 for running, you should edit symlinks not alias.
-
AlexeyGy almost 5 yearsyou should set the pip alias not to pip3.6 specifically, instead set it to pip3: ``alias pip='pip3'
-
Ani almost 5 yearsadding -> alias python='python3' in ~/.bash_profile file worked for me.
-
jjwdesign almost 5 yearsFor 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 over 4 yearsThis did not work for me. After following this still python2 is invoked by default.
-
Dani over 4 yearsWould it still break things now in Catalina?
-
Santosh Kumar over 4 yearsNope, its bash thing. Nothing to do with the operating system.
-
Santosh Kumar over 4 yearsDoesn'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. over 4 yearsThe 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 over 4 yearsIf 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 about 4 yearsThis worked for me but using "alias python=python3"
-
Ashwin R about 4 yearsThis will temporarily set python version for that terminal instance.
-
Ricky Aguilar about 4 yearsDo 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 about 4 yearsDo 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 almost 4 yearsWorks great (Reference)
-
TimewiseGamgee almost 4 yearsThis feels like the cleaner approach. I set mine to python 3.5 and it worked.
-
Craig Conover over 3 yearsNot sure why you got a down vote. This worked for me! Thanks for posing this. I upvoted you to cancel that down vote ;)
-
Pei over 3 yearsI had to close terminal and open a new one to get it working in macOS
-
Robin over 3 yearsWhilst 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 over 3 yearsOn Catalina (10.15.17), I get:
Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.
-
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 almost 3 yearsthis does not work for me
-
Nikhil Das Nomula almost 3 yearsI had to do this in my case ln -s -f /usr/local/Cellar/[email protected]/3.8.11/bin /usr/local/bin/python3
-
iDevSpread over 2 yearsIt worked for me,tkx
-
Xitcod13 over 2 yearsError: Unknown command: switch
-
sohammondal over 2 yearsthis 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 runsudo ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip
and then pip worked. -
sknight over 2 years@sohammondal Thanks you. I appreciate your input.
-
Mustafa about 2 yearsln: /usr/bin/python: Operation not permitted
-
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 addingexport PATH="/Applications/anaconda3/envs/py3.10/bin:$PATH"
it works aftersource
. However, once I quite the terminal and reopen, it turns out the base is still py3.8. and i need tosource
again. Which is unresonable. Since I canconda activate py3.10
. Have u find another way to tackle this? -
L. Heinrichs almost 2 yearsFor me it looked like this:
brew link [email protected]
. Thanks for hints! -
Rohan Kumara almost 2 yearsif you already installed visual studio code it will be easy to use.
code ~/.bash_profile
instead of using.vi ~/.bash_profile
. -
Rai Rz almost 2 yearsJust this code worked for my Mac, thanks