Mapping python to Python 3 on Your Mac

When you install Python 3 on a Mac, it does not update the python command to use Python 3 instead of Python 2. Instead, to run Python 3, you have to use the python3 command. Here is how you how to fix this.

First, check your Python version:

Nats-MBP:~ natdunn$ python -V
Python 2.7.15

We want that to say python 3. something.

What shell are you using?

First, determine what shell you are using.

Open Terminal and look at the last character of the prompt. If it’s a dollar sign ($), you’re most likely using bash. If it’s a percentage sign (%), you’re most likely using zsh. You can confirm by running:

echo $SHELL

Update your profile

First, change into your Home directory:

cd ~

Depending on which shell you are using, enter one of the following:

nano .bash_profile
nano .zshrc

This will open the nano editor. If it has content already, scroll to the bottom using your down arrow key.

Add the following line:

alias python=python3

Make sure to save (write out) by pressing Ctrl+O then Enter. Then press Ctrl+X and Enter to exit nano.

Restart Terminal.

Run python -V to see check the version:

Nats-MBP:~ natdunn$ python -V
Python 3.9.5

You’re good to go.

Written by Nat Dunn. Follow Nat on Twitter.


Related Articles

  1. Understanding Python’s __main__ variable
  2. How to find all your Python installations on Windows (and Mac)
  3. Python Virtual Environments with venv
  4. Mapping python to Python 3 on Your Mac (this article)