7

I am currently trying to change my default Python version to Python3. This proves to be harder than expected. I have already tried the following things:

  1. I have tried to change the alias by doing alias python python3 but this seems to just change it for the running Terminal session
  2. I have installed Python3 again with Homebrew and tried to change the directory within the zshellruncommand by defining it myself and editing it in vim to be honest I don't really understand the process and just followed along with a tutorial but it didn't work.
  3. I tried the command ln -s -f /opt/homebrew/bin/python3 /usr/bin/python to change the directories but it returned ln: /usr/bin/python: Operation not permitted

to deal with this problem I have already given the Terminal full access to the local harddrive but it is still not working properly

Hoping for some advise, thanks in advance

Jake
  • 117
  • 1
  • 5
  • 1
    Until the OS itself decides that `python` should refer to a Python 3 interpreter, you are better off working in a virtual environment created from whichever Python you want to use. – chepner Nov 26 '21 at 15:46
  • 1
    for `ln -s` you want to try with `/usr/local/bin/python` instead. you cannot modify `/usr/bin/python` on mac - see also the post [here](https://stackoverflow.com/a/65297862/10237506). – rv.kvetch Nov 26 '21 at 15:46
  • 1
    Activation of said virtual environment can be done from `.zshrc` so that it's automatically available in any interactive instance of `zsh`. – chepner Nov 26 '21 at 15:46

2 Answers2

2

First make sure the installed version is linked:

brew link python

then on your .bash_profile or .bashrc (.zshrc if you're using zsh) write this:

export PATH="/usr/local/opt/python@X.Y/libexec/bin:$PATH"

change X and Y to your python version

Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
2

I think it's easier to just change your user shell environment instead of changing the system level environment.

vi ~/.bash

Then type

alias python=python3

Save your file by pressing Esc -> type :wq

Update your shell environment

source ~/.bash

Check your Python version.

python -V

Final result

Python 3.9.10

This works in my past few versions MacOS.

hatted
  • 1,535
  • 1
  • 15
  • 26