loading...

Change Python Version Mac How to set Python3 as a default python version on MacOS?

malwarebo profile image Irfan Updated on ・2 min read

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

Look where it is installed.

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

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

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

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

python --version

You will get this:

Python 3.7.5

Hah! That's it. Happy coding!!

Posted on Dec 4 '19 by:

malwarebo profile

Irfan

@malwarebo

Hack programming languages and read.

Discussion

markdown guide
 

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?

 

Hi, sorry for late reply.

What is the output of this command you get?

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

 

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

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

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

Hey, what is your MacOS version?

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

Awesome, glad that you fixed it.

I'm on MacOS Catalina and it worked. Strange

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

 

Another option is to use a version manager like pyenv. You can install with homebrew and it will allow you to install specific versions of Python at different levels (globally use Python 3.7 but use 2.7 in a specific directory, etc).

It works by adding a shim to the $PATH and calling the defined version. I prefer this approach because you don't run into weird issues by changing the Python reference directly.

 

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!

 

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!

 

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.

 

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?

 

Hey could you try this first then run the command?

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

 

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 ?

 

Hey try this:

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

then

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

That is worked Irfan. Thanks for your help .

 

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)

 

Did you check after starting a new terminal session?

 

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!

 

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.

 

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

 

Hi,
I am still stuck
(base) Carolines-MBP:~ carolinekibat$ ls -1 /usr/local/bin/python*
/usr/local/bin/python
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.8
/usr/local/bin/python3.8-config
(base) Carolines-MBP:~ carolinekibat$ ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
ln: /usr/local/bin/python: Permission denied

Then I tried pip
(base) Carolines-MBP:~ carolinekibat$ python --version
Python 2.7.14 :: Anaconda, Inc.
(base) Carolines-MBP:~ carolinekibat$ pip3 --version
pip 20.1.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
(base) Carolines-MBP:~ carolinekibat$ pip3 install --upgrade pip
Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (20.1.1)
(base) Carolines-MBP:~ carolinekibat$ which pip
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip
(base) Carolines-MBP:~ carolinekibat$ sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
Password:

close and open terminal

Last login: Sat Jun 27 20:32:57 on ttys000
You have new mail.
(base) Carolines-MBP:~ carolinekibat$ python --version
Python 2.7.14 :: Anaconda, Inc.
(base) Carolines-MBP:~ carolinekibat$

Why doesn't it change to python 3?

 

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 ?

 

I tried this and now get this when I run python --version:

Usage: /usr/local/bin/python [--prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--abiflags|--configdir|--embed]

Anyone know what's going on?

EDIT: I fixed this. This step: "Change the default python symlink to the version you want to use from above." should be better explained as to what the command should be. I had changed the wrong part of it

 

Thank you Steven! I have modified the post. Let me know if anything else needs to be changed.

 

Thank you! I've being looking for a straight forward article to fix symlinks with Apple's lovely handy work.

 
 
 

Thank you so much. I assumed homebrew made that link for me. I guess either it did or it got overwritten. You've solved the problem that I've spent at least an hour on.

 

Worked like charm on Macbook Pro 2017, 13" MacOS Catalina. Thank you

 

Thanks for the solution, So easily and clean!!

 

this helped me and solved my problem. thank you!

 

I'm wondering why the command "which python" still returns usr/local/bin/python (which is in my case python 2), but "python -V" or "python --version" both return Python 3?

 

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