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.
Related Articles
- Understanding Python’s __main__ variable
- How to find all your Python installations on Windows (and Mac)
- Associate Python Files with IDLE
- Python Virtual Environments with venv
- Mapping python to Python 3 on Your Mac (this article)