2

My goal is to make sure that python refers to python3 in my mac terminal. What i have tried so far are the following two attempts

1. Adding an alias in .zshrc

alias python=python3

after re sourcing my .zshrc file this allowed me to type python --version and get python3.

This does not work for scripts as they specify #!/usr/bin/env python and completely ignore my aliases.

2. Adding a symlink from /usr/local/bin/python to /usr/bin/python3

This is when it gets weird. Here are the output of a few commands

python --version -> python 2.7

which python -> python /usr/local/bin/python

/usr/local/bin/python --version -> python 2.7

ls -l /usr/local/bin/python -> /usr/local/bin/python -> /usr/bin/python3

/usr/bin/python3 --version -> python 3.8

What is going wrong here? Why is the symlink apparently correct but the wrong version of python is still showing when using it?

I found a similar question on an old apple but the answer doesn't really address the weirdness of the symlink, nor doesnt it solve the problem.

I'd really like to understand why this is happening AND find a solutionn or a proper way to get my main python version to be python 3.

Thanks!

3
  • Is it too impractical to just update your scripts to point to the version you want? For example, change #!/usr/bin/env python to #!/usr/bin/env python3? Unless you’re sure of everything that makes use of a particular version, changing it by completely disallowing it via soft links or something could have unintended consequences.
    – Ackack
    Jan 17, 2022 at 2:34
  • I can do that, and that is what i ended up doing. I am still curious about the weirdness with symlinks tho. any ideas ? Jan 18, 2022 at 15:43
  • Run hash python in case your shell cached that it was in /usr/bin/python
    – user253751
    Jul 28, 2022 at 18:53

1 Answer 1

1

check current python version:
$ python --version: output Python 2.* OR Command 'python' not found

check current python3 version:
$ python3 --version: output Python 3.*

Check where python3 is located:
$ which python3: output /usr/bin/python3

Create your sym link here:
$ sudo ln -sf /usr/bin/python3 /usr/bin/python
where /usr/bin/python is the new executable

exit terminal + reopen

Now check your python executable is actually running python3

$ python --version: output Python 3.*

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 24, 2022 at 5:02
  • Well, the whole problem is that creating a link is NOT allowed: ln: /usr/bin/python: Operation not permitted Oct 12, 2023 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .