DEV Community

Irfan
Irfan

Posted on • Updated 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!!

Latest comments (84)

Collapse
 
kryllyxofficial01 profile image
Kryllyx

Thanks dude. It worked.

Collapse
 
malwarebo profile image
Irfan

Glad it helped :)

Collapse
 
davidfp profile image
David Fernández Puentes

Good Job, It works for me. Thanks!

Collapse
 
leelou_ profile image
leelou _

I am frustrated... Nothing worked for me. Am i doing something wrong? (macOs 10.13.6)

Last login: Wed Jun 8 17:13:10 on ttys000
(base) Leelou:~ leelou$ ls -l /usr/local/bin/python*
lrwxr-xr-x 1 leelou admin 24 Jun 8 17:12 /usr/local/bin/python -> /usr/local/bin/python3.7
lrwxr-xr-x 1 root wheel 71 Oct 30 2019 /usr/local/bin/python-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-32
lrwxr-xr-x 1 root wheel 75 Oct 30 2019 /usr/local/bin/python-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-config
lrwxr-xr-x 1 leelou admin 25 Jun 8 16:24 /usr/local/bin/python.old -> /usr/local/bin/python3.10
lrwxr-xr-x 1 root wheel 69 Oct 30 2019 /usr/local/bin/python2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2
lrwxr-xr-x 1 root wheel 72 Oct 30 2019 /usr/local/bin/python2-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-32
lrwxr-xr-x 1 root wheel 76 Oct 30 2019 /usr/local/bin/python2-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-config
lrwxr-xr-x 1 leelou admin 25 Jun 8 16:55 /usr/local/bin/python2.7 -> /usr/local/bin/python3.10
lrwxr-xr-x 1 root wheel 74 Oct 30 2019 /usr/local/bin/python2.7-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-32
lrwxr-xr-x 1 root wheel 78 Oct 30 2019 /usr/local/bin/python2.7-config -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 70 Jun 8 16:42 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3
lrwxr-xr-x 1 root wheel 72 Sep 26 2017 /usr/local/bin/python3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3-32
lrwxr-xr-x 1 root wheel 77 Jun 8 16:42 /usr/local/bin/python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3-config
lrwxr-xr-x 1 root wheel 78 Jun 8 16:42 /usr/local/bin/python3-intel64 -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3-intel64
lrwxr-xr-x 1 root wheel 73 Jun 8 16:42 /usr/local/bin/python3.10 -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
lrwxr-xr-x 1 root wheel 80 Jun 8 16:42 /usr/local/bin/python3.10-config -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10-config
lrwxr-xr-x 1 root wheel 81 Jun 8 16:42 /usr/local/bin/python3.10-intel64 -> ../../../Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10-intel64
lrwxr-xr-x 1 root wheel 71 Sep 26 2017 /usr/local/bin/python3.6 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
lrwxr-xr-x 1 root wheel 74 Sep 26 2017 /usr/local/bin/python3.6-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-32
lrwxr-xr-x 1 root wheel 78 Sep 26 2017 /usr/local/bin/python3.6-config -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
lrwxr-xr-x 1 root wheel 72 Sep 26 2017 /usr/local/bin/python3.6m -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m
lrwxr-xr-x 1 root wheel 79 Sep 26 2017 /usr/local/bin/python3.6m-config -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
lrwxr-xr-x 1 root wheel 78 Jun 19 2019 /usr/local/bin/python3.7-config -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config
lrwxr-xr-x 1 root wheel 72 Jun 19 2019 /usr/local/bin/python3.7m -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m
lrwxr-xr-x 1 root wheel 79 Jun 19 2019 /usr/local/bin/python3.7m-config -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m-config
lrwxr-xr-x 1 root wheel 69 Oct 30 2019 /usr/local/bin/pythonw -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw
lrwxr-xr-x 1 root wheel 72 Oct 30 2019 /usr/local/bin/pythonw-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw-32
lrwxr-xr-x 1 root wheel 70 Oct 30 2019 /usr/local/bin/pythonw2 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2
lrwxr-xr-x 1 root wheel 73 Oct 30 2019 /usr/local/bin/pythonw2-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2-32
lrwxr-xr-x 1 root wheel 72 Oct 30 2019 /usr/local/bin/pythonw2.7 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 75 Oct 30 2019 /usr/local/bin/pythonw2.7-32 -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7-32

/usr/local/bin/python3.7:
total 0
drwxr-xr-x 13 leelou wheel 416 Jun 19 2019 site-packages
(base) Leelou:~ leelou$ pip3 --version
pip 22.1.2 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
(base) Leelou:~ leelou$ pip3 install --upgrade pip
Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (22.1.2)
(base) Leelou:~ leelou$ which pip
/Library/Frameworks/Python.framework/Versions/3.10/bin/pip
(base) Leelou:~ leelou$ sudo ln -s -f /usr/local/bin/python3.10 /usr/local/bin/python
Password:
(base) Leelou:~ leelou$ python --version
Python 2.7.17
(base) Leelou:~ leelou$

Collapse
 
rwheeler7864 profile image
Robert F Wheeler

didn't work.
nice try though

ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python
(venv) (base) robwheeler@Roberts-MBP practice-tools % python --version
Python 3.7.3
(venv) (base) robwheeler@Roberts-MBP practice-tools %

Collapse
 
metacowboy profile image
Metaeditor

How i could easily switch back to 2.7 if needet for one or other old project ?

Collapse
 
nick_sassow_afa10f1ecef7e profile image
Nick Sassow

Tried
pip3 --version
pip 21.3.1 from /Users/nick/Library/Python/3.8/lib/python/site-packages/pip (python 3.8)
pip --version
zsh: command not found: pip

Collapse
 
nick_sassow_afa10f1ecef7e profile image
Nick Sassow

tried every command stream in the posts... No luck:

nick2@NWS-Pro-21 ~ % python3 --version
Python 3.8.9
nick2@NWS-Pro-21 ~ % python --version
Python 2.7.18
nick2@NWS-Pro-21 ~ % python -V
Python 2.7.18
python3 --version
Python 3.8.9

this is on macOS Monterrey 12.0.1

i am an oldie 62 ex unix 5.3.1 and a newbie at same time...

Collapse
 
clemoseitano profile image
Clement Osei Tano

Awesome, thanks!

Collapse
 
dimazz profile image
dimazz

// That worked for me on Big Sur

exec $SHELL -l
which python
/usr/local/bin/python
python --version

Collapse
 
zimanaanalytics profile image
Zimana Analytics #BlackInDataWeek • Edited

This worked for me -- was able to connect to Python 3.9, so thank you for sharing this. I am not sure why Apple still ships with Python 2 (I have the M1 MacBook pro and still had this!) ;-)

Again, thank you!

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
 
paulocezzar profile image
P S

I have solved that by installing the script Update Shell Profile.command

in the python file found in Applications.

Collapse
 
putroandrianto profile image
Andrianto Cahyono Putro

worked! thanks

Collapse
 
fxgx99 profile image
C Martin • Edited

Hi I am having a similar problem but i just recently updated to MacOs Big sure and I have been trying to install aws cli and ran into this python version issue. Below is the output of ls -l /usr/local/bin/python*: however i am still getting Python 3.7.10 as my default and I will like to change that to python3.9

The first 10 lines of ls -l /usr/local/bin/python* is below :
rwxr-xr-x root admin 24 B Wed Apr 7 09:42:43 2021  python ⇒ /usr/local/bin/python3.9

rwxr-xr-x christian wheel 71 B Sun Nov 8 08:08:51 2015  python-32 ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-32

rwxr-xr-x christian wheel 75 B Sun Nov 8 08:08:51 2015  python-config ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python-config

rwxr-xr-x christian wheel 69 B Sun Nov 8 08:08:51 2015  python2 ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2

rwxr-xr-x christian wheel 72 B Sun Nov 8 08:08:51 2015  python2-32 ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-32

rwxr-xr-x christian wheel 76 B Sun Nov 8 08:08:51 2015  python2-config ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2-config

rwxr-xr-x christian wheel 71 B Sun Nov 8 08:08:51 2015  python2.7 ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

rwxr-xr-x christian wheel 74 B Sun Nov 8 08:08:51 2015  python2.7-32 ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-32

rwxr-xr-x christian wheel 78 B Sun Nov 8 08:08:51 2015  python2.7-config ⇒ ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config

rwxr-xr-x root wheel 69 B Wed Apr 7 07:59:49 2021  python3 ⇒ ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3

Collapse
 
malwarebo profile image
Irfan

Did you try this:

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

Provided you have installed Python 3.9.

Collapse
 
crimsonkida profile image
CrimsonKidA

Had to also run this:

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

Thanks, all

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