6

Monterey(M1) has python2 and python3 preinstalled. But the python2 is set as default, I want to change it to python3. How to change the default version to python3? enter image description here

enter image description hereIn my Macbook Air having

ADITYA RAJ
  • 123
  • 1
  • 2
  • 8
  • 1
    not sure if it would break some existing functionality under the hood by changing `python` to point to `python3`. what is wrong with just typing `python3`? – gold_cy Jan 09 '22 at 13:39
  • you would need to add symbolic(as suggested by Saliou DJIBRILA) and then export that. These two posts will give you some more details [python3 before python2](https://stackoverflow.com/questions/61941873/how-to-set-python3-as-a-default-python-version-on-mac) and [symbolic link](https://apple.stackexchange.com/questions/115646/how-can-i-create-a-symbolic-link-in-terminal) – simpleApp Jan 09 '22 at 14:15
  • 2
    [Please do not upload images of code/errors/text when asking a question.](//meta.stackoverflow.com/q/285551) – MisterMiyagi Jan 09 '22 at 14:25

3 Answers3

13

If you are on Mac M1

Steps

1. $ brew install python
2. $ sudo ln -s -f /opt/homebrew/bin/python3 /usr/local/bin/python
3. Restart your terminal.
4. $ python --version

If you are on Intel Mac

Steps

1. $ brew install python
2. $ ln -s -f /usr/local/bin/python3.<your version> /usr/local/bin/python
3. Close Terminal and test version 
4. $ python --version
Dharman
  • 30,962
  • 25
  • 85
  • 135
Alok Tripathi
  • 874
  • 10
  • 9
4

You can use symbolic link: ln -s -f /usr/local/bin/python3 /usr/local/bin/python

2

From the information provided it is not possible to say if /usr/bin/python is already a symlink. You can check all the python versions in that directory and see if it is a symlink or not.

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

If python is an existing symlink

In the case that python is a symlink, you should be able to change the symlink to link to python3 as already suggested here, -s for soft, -f for force to overwrite an existing symlink.

ln -sf /usr/bin/python3 /usr/local/bin/python

If the operation is not permitted you will need to create it as root. If this is good solution I do not know, so use my answer with precaution.

sudo -i
ln -sf /usr/bin/python3 /usr/local/bin/python

Check if the right version is recognised

where python
python -V

From MacOS Monterey 12.3

From MacOS Monterey Version 12.3, python2 is no longer preinstalled, and is shipped with Python 3.8.9. Therefore python is not in use and a symlink can be used without concerns regarding python2.

python -V
zsh: command not found: python
python2 -V
zsh: command not found: python2
python3 -V
Python 3.8.9
Line
  • 58
  • 5