DEV Community

Irfan
Irfan

Posted on • Edited on

Change Python Version Mac

How to set Python3 as a default python version on MacOS?

By default MacOS ships with Python-2.-. But, I guess most of us have long back started to work with Python-3 and it is very irritating to run python3 every time instead of python in terminal. Here is how to do this.

Open the terminal (bash or zsh) whatever shell you are using.

Install python-3 using Homebrew (https://brew.sh).

brew install python
Enter fullscreen mode Exit fullscreen mode

Look where it is installed.

ls -l /usr/local/bin/python*
Enter fullscreen mode Exit fullscreen mode

The output is something like this:

lrwxr-xr-x  1 irfan  admin  34 Nov 11 16:32 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
lrwxr-xr-x  1 irfan  admin  41 Nov 11 16:32 /usr/local/bin/python3-config -> ../Cellar/python/3.7.5/bin/python3-config
lrwxr-xr-x  1 irfan  admin  36 Nov 11 16:32 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.5/bin/python3.7
lrwxr-xr-x  1 irfan  admin  43 Nov 11 16:32 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.5/bin/python3.7-config
lrwxr-xr-x  1 irfan  admin  37 Nov 11 16:32 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.5/bin/python3.7m
lrwxr-xr-x  1 irfan  admin  44 Nov 11 16:32 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.5/bin/python3.7m-config
Enter fullscreen mode Exit fullscreen mode

Change the default python symlink to the version you want to use from above.
Note that, we only need to choose the one that end with python3.*. Please avoid using the ones' that end with config or python3.*m or python3.*m-config.

Below command shows how it should be done:

ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
Enter fullscreen mode Exit fullscreen mode

Close the current terminal session or keep it that way and instead open a new terminal window (not tab). Run this:

python --version

Enter fullscreen mode Exit fullscreen mode

You will get this:

Python 3.7.5
Enter fullscreen mode Exit fullscreen mode

Hah! That's it. Happy coding!!

Top comments (84)

Collapse
 
tedonmain profile image
tedro • Edited

Worked! Thanks!

I'm not saying anyone would want to install Python 3 without brew, but if they did, pip3 gets installed:
$ pip3 --version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
$ pip3 install --upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54... (1.4MB)
|████████████████████████████████| 1.4MB 1.7MB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2

$ which pip
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

Now, Irfan's handy command:
sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python

close/open terminal

$ python --version
Python 3.8.2

yay Irfan!

Collapse
 
apoorveesinha profile image
APOORVEE SINHA

Hey! I tried following your above-mentioned method .. but I don't know how the path is different ...Although this method worked but will that path create any problem later on?
On my mac
which pip results in
/usr/local/bin/pip
instead of
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

apoorveesinha@Apoorvees-Air ~ % python --version
Python 2.7.16
apoorveesinha@Apoorvees-Air ~ % python3 --version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % pip3 --version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
apoorveesinha@Apoorvees-Air ~ % pip3 install --upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54... (1.4MB)Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
Password:
apoorveesinha@Apoorvees-Air ~ %

close/open terminal

apoorveesinha@Apoorvees-Air ~ % python --version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % python3 --version
Python 3.8.2

Please help!

Collapse
 
tedonmain profile image
tedro

I’m happy to read that the steps worked for you. The subject of the post I responded to was setting up Python 3 to be the default. From what I can tell, that is what you now have. My goal was to show how I did this directly versus using a package manager like Brew, but not everyone is comfortable with this approach. It looks like you are good to go if you want to work in Python 3 by default.

Collapse
 
garciatavo profile image
Tav0

export PATH=/usr/local/opt/python3/libexec/bin:$PATH

Thread Thread
 
crimsonkida profile image
CrimsonKidA

This worked for me! THANK YOU! :D

Collapse
 
nishu_2811 profile image
Nishant Ranjan

Thank you for sharing. It saved my day.

Collapse
 
tedonmain profile image
tedro

-ha! good to hear. I receive a message like yours about once per month. seems like a niche crowd we are in, hoping to avoid brew on our mac. it was exactly what I needed also.

Collapse
 
elpidabantra profile image
elpidabantra

Oh god! Thank you so much for your answer, I had tried everything and I was going crazy with that thing, I did the same with Python 3.7 and I finally see:
$ python --version
Python 3.7.8

Collapse
 
tedonmain profile image
tedro

Great! I'm happy it help you.

Collapse
 
ocalde profile image
Oscar Calderon

Hello Irfan, thanks for the quick tutorial. I have Catalina, and it didn't work for me. I was able to install python using brew, but even if I create the symlink in /usr/local/bin, it seems it is taking the symlink in /usr/bin which points to python2.7.

When I try to change that symlink, I get the message ln: python: Operation not permitted

Any ideas about what can be done?

Collapse
 
malwarebo profile image
Irfan

Hi, sorry for late reply.

What is the output of this command you get?

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

Collapse
 
monmoscovitz profile image
Mon Moscovitz

Output seem like this (Catalina - 10.15.3)
lrwxr-xr-x 1 hoangson admin 24 Feb 21 10:40 /usr/local/bin/python -> /usr/local/bin/python3.7
lrwxr-xr-x 1 hoangson admin 34 Dec 17 20:48 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
lrwxr-xr-x 1 hoangson admin 41 Dec 17 20:48 /usr/local/bin/python3-config -> ../Cellar/python/3.7.5/bin/python3-config
lrwxr-xr-x 1 hoangson admin 36 Dec 17 20:48 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.5/bin/python3.7
lrwxr-xr-x 1 hoangson admin 43 Dec 17 20:48 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.5/bin/python3.7-config
lrwxr-xr-x 1 hoangson admin 37 Dec 17 20:48 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.5/bin/python3.7m
lrwxr-xr-x 1 hoangson admin 44 Dec 17 20:48 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.5/bin/python3.7m-config

Thread Thread
 
malwarebo profile image
Irfan

It seems you already have 3.7 as a default version. See first line of the output.

Thread Thread
 
vincentfeng0101 profile image
Vincent

hello sir, I have this output with command: ls -l /usr/local/bin/python*, but when I type python --version, output is still python2. could you please help me

lrwxr-xr-x 1 djf admin 24 Mar 21 15:40 /usr/local/bin/python -> /usr/local/bin/python3.7
lrwxr-xr-x 1 djf admin 34 Jun 20 2019 /usr/local/bin/python3 -> ../Cellar/python/3.7.3/bin/python3
lrwxr-xr-x 1 djf admin 41 Jun 20 2019 /usr/local/bin/python3-config -> ../Cellar/python/3.7.3/bin/python3-config
lrwxr-xr-x 1 djf admin 36 Jun 20 2019 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.3/bin/python3.7
lrwxr-xr-x 1 djf admin 43 Jun 20 2019 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.3/bin/python3.7-config
lrwxr-xr-x 1 djf admin 37 Jun 20 2019 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.3/bin/python3.7m
lrwxr-xr-x 1 djf admin 44 Jun 20 2019 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.3/bin/python3.7m-config

Thread Thread
 
malwarebo profile image
Irfan

Hey, what is your MacOS version?

Thread Thread
 
vincentfeng0101 profile image
Vincent

thank you for your reply, i have fix my problem.

Thread Thread
 
malwarebo profile image
Irfan

Awesome, glad that you fixed it.

Thread Thread
 
alt_blr_ceo profile image
Just Another Security Researcher

I'm on MacOS Catalina and it worked. Strange

Thread Thread
 
xianyang27200 profile image
xianyang27200

how did you fixed it please i have the same issue as yours 100%

Thread Thread
 
milah3 profile image
Milah3

Try the original answer and then restart your window. If that doesn't work, try it again without the last part and then restart your window:

ln -s -f /usr/local/bin/python3.7

Collapse
 
davidfp profile image
David Fernández Puentes

Good Job, It works for me. Thanks!

Collapse
 
alexneville47 profile image
alexneville47 • Edited

I already have homebrew and python 3.7.8 installed and when i followed the steps you provided it still had python 2.7.16 instead of 3.7.8 and i followed every step any idea whats wrong? by the way im on the newest macOS because i just got this laptop a few days ago

Collapse
 
chashchindmitry profile image
Chashchin-Dmitry

dmitrij$ ln -s -f /usr/local/bin/python3.8.2 /usr/local/bin/python
ln: /usr/local/bin/python: Permission denied

Excuse me, how can I avoid or get permission?

Collapse
 
malwarebo profile image
Irfan

Hey could you try this first then run the command?

sudo chown -R $(whoami) /usr/local/bin/python

Collapse
 
bubasar profile image
Bubasar

Hi , Dmitry
Could you solve the problem you have above ? I have the same problem and if you managed to solve it pls share me your way ?

Collapse
 
malwarebo profile image
Irfan

Hey try this:

sudo chown -R $(whoami) /usr/local/python

then

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

Thread Thread
 
bubasar profile image
Bubasar

That is worked Irfan. Thanks for your help .

Collapse
 
apoorveesinha profile image
APOORVEE SINHA

I have followed all the steps but python --version gives Python 2.7.16 as output. Please help.

ls -l /usr/local/bin/python*
lrwxr-xr-x 1 apoorveesinha admin 24 Mar 30 04:57 /usr/local/bin/python -> /usr/local/bin/python3.7
lrwxr-xr-x 1 apoorveesinha admin 34 Mar 30 04:55 /usr/local/bin/python3 -> ../Cellar/python/3.7.7/bin/python3
lrwxr-xr-x 1 apoorveesinha admin 41 Mar 30 04:55 /usr/local/bin/python3-config -> ../Cellar/python/3.7.7/bin/python3-config
lrwxr-xr-x 1 apoorveesinha admin 36 Mar 30 04:55 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.7/bin/python3.7
lrwxr-xr-x 1 apoorveesinha admin 43 Mar 30 04:55 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.7/bin/python3.7-config
lrwxr-xr-x 1 apoorveesinha admin 37 Mar 30 04:55 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.7/bin/python3.7m
lrwxr-xr-x 1 apoorveesinha admin 44 Mar 30 04:55 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.7/bin/python3.7m-config
lrwxr-xr-x 1 root wheel 71 Mar 30 04:37 /usr/local/bin/python3.8 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
lrwxr-xr-x 1 root wheel 78 Mar 30 04:37 /usr/local/bin/python3.8-config -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-config
apoorveesinha@Apoorvees-Air ~ % ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
apoorveesinha@Apoorvees-Air ~ % python --version

Python 2.7.16
apoorveesinha@Apoorvees-Air ~ % pip3 --version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)

Collapse
 
malwarebo profile image
Irfan

Did you check after starting a new terminal session?

Collapse
 
apoorveesinha profile image
APOORVEE SINHA

Yes, I did.
Moreover, I tried following tedro's method above in the comment section.. but I don't know how the path is different ...Although this method worked but will that path create any problem later on?
On my mac
which pip results in
/usr/local/bin/pip
instead of
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

Last login: Thu Apr 16 18:09:08 on ttys000
apoorveesinha@Apoorvees-Air ~ % python --version
Python 2.7.16
apoorveesinha@Apoorvees-Air ~ % python3 --version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % pip3 --version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
apoorveesinha@Apoorvees-Air ~ % pip3 install --upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54... (1.4MB)Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
Password:
apoorveesinha@Apoorvees-Air ~ %

close/open terminal

apoorveesinha@Apoorvees-Air ~ % python --version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % python3 --version
Python 3.8.2

Please help!

Collapse
 
ariefbaidowi profile image
Bai

Hi, I have problem with my installation. I use OS X El Capitan (10.11.6).

$ ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
ln: /usr/local/bin/python: Permission denied
users-mbp:bin Bai$ ls -l /usr/local/bin/python*
lrwxr-xr-x 1 root wheel 68 Apr 23 00:34 /usr/local/bin/python -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python
lrwxr-xr-x 1 root wheel 75 Apr 23 00:34 /usr/local/bin/python-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-config
lrwxr-xr-x 1 root wheel 69 Apr 23 00:34 /usr/local/bin/python2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2
lrwxr-xr-x 1 root wheel 76 Apr 23 00:34 /usr/local/bin/python2-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-config
lrwxr-xr-x 1 root wheel 71 Apr 23 00:34 /usr/local/bin/python2.7 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 78 Apr 23 00:34 /usr/local/bin/python2.7-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 69 Apr 23 01:21 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3
lrwxr-xr-x 1 root wheel 76 Apr 23 01:21 /usr/local/bin/python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3-config
lrwxr-xr-x 1 root wheel 71 Apr 23 01:21 /usr/local/bin/python3.8 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
lrwxr-xr-x 1 root wheel 78 Apr 23 01:21 /usr/local/bin/python3.8-config -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8-config
lrwxr-xr-x 1 root wheel 69 Apr 23 00:34 /usr/local/bin/pythonw -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw
lrwxr-xr-x 1 root wheel 70 Apr 23 00:34 /usr/local/bin/pythonw2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2
lrwxr-xr-x 1 root wheel 72 Apr 23 00:34 /usr/local/bin/pythonw2.7 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

Can u help me to solve it ?

Collapse
 
claracavalcante profile image
Ana Clara

Thank you for the tutorial!

One thing I am not sure about is pip.

For some reason when I use "pip --version" the outcome is this:
pip 21.2.4 from /Library/Python/3.8/site-packages/pip (python 3.8)

and when I use "pip3 --version", the outcome is:
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Any ideas on why are they using different python releases? And how to update it.

Collapse
 
claracavalcante profile image
Ana Clara

I fixed this by reinstalling pip with the following the commands:

curl bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Collapse
 
srinivasaraog30 profile image
srinivasarao.gurram

Hi Irfan,
Can you help,

lrwxr-xr-x 1 sgurram admin 24 May 14 19:17 /usr/local/bin/python -> /usr/local/bin/python3.7
lrwxr-xr-x 1 sgurram admin 39 May 7 14:14 /usr/local/bin/python-build -> ../Cellar/pyenv/1.2.18/bin/python-build
lrwxr-xr-x 1 root wheel 75 May 14 16:28 /usr/local/bin/python-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-config
lrwxr-xr-x 1 root wheel 69 May 14 16:28 /usr/local/bin/python2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2
lrwxr-xr-x 1 root wheel 76 May 14 16:28 /usr/local/bin/python2-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-config
lrwxr-xr-x 1 root wheel 71 May 14 16:28 /usr/local/bin/python2.7 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 78 May 14 16:28 /usr/local/bin/python2.7-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 sgurram admin 34 May 14 16:26 /usr/local/bin/python3 -> ../Cellar/python/3.7.7/bin/python3
lrwxr-xr-x 1 sgurram admin 41 May 14 16:26 /usr/local/bin/python3-config -> ../Cellar/python/3.7.7/bin/python3-config
lrwxr-xr-x 1 sgurram admin 36 May 14 16:26 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.7/bin/python3.7
lrwxr-xr-x 1 sgurram admin 43 May 14 16:26 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.7/bin/python3.7-config
lrwxr-xr-x 1 sgurram admin 37 May 14 16:26 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.7/bin/python3.7m
lrwxr-xr-x 1 sgurram admin 44 May 14 16:26 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.7/bin/python3.7m-config
lrwxr-xr-x 1 root wheel 69 May 14 16:28 /usr/local/bin/pythonw -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw
lrwxr-xr-x 1 root wheel 70 May 14 16:28 /usr/local/bin/pythonw2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2
lrwxr-xr-x 1 root wheel 72 May 14 16:28 /usr/local/bin/pythonw2.7 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

my version is mac os catalina 10.15.4
sgurram@MC02Z52VDLVDQ ~ % python --version
Python 2.7.18
can u help me

Collapse
 
fishbacp profile image
fishbacp • Edited

I found your tip extremely useful to solve the following problem I posted on stackoverflow:

I had a simple .py script that used tkinter, which I wanted to use to create a Mac OS app. I did my work using python 3.7 and IDLE. The script worked fine when run in IDLE, but the app I created using py2app and clear documentation at (py2app.readthedocs.io/en/latest/tu...) would not function,

I updated my Mac's python version from 2.7.16 to 3.7 and the app worked like a charm! Thanks--this made my day.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.