How to Map ‘python’ to ‘python3’ on Mac

In this tutorial, we’ll explore how to configure your machine to use the python command instead of python3.

When you install Python 3 or the latest version of Python on your Mac, it’s typically executed using the python3 command. However, if you prefer to use Python 3 while running it with just the python command, this guide is for you.

Python3 Mac Terminal

Before we proceed with the tutorial, I assume that you already have Python installed on your Mac. If you haven’t installed it yet, please refer to this guide on how to install Python on your Mac.

Let’s dive in.

Mapping ‘python’ to ‘python3’

To change the default python3 command on your Mac to point to python, you can create an alias. This is done by modifying your shell’s configuration file. Here’s how to do it:

Step 1: Open Terminal

You can find it in the Applications folder under Utilities, or search for it using Spotlight.

Step 2: Determine Your Shell

macOS uses either bash or zsh as the default shell. You can determine which one you’re using by running echo $SHELL in the terminal.

Step 3: Edit the Configuration File
  • If you’re using bash, you’ll edit ~/.bash_profile, ~/.bashrc, or ~/.profile.
  • If you’re using zsh, you’ll edit ~/.zshrc.
Step 4: Add an Alias

Open the appropriate file in a text editor, such as Nano, by typing nano ~/.bash_profile or nano ~/.zshrc (depending on your shell).

Add the following line to the file: alias python='python3'.

Save and close the file (in Nano, this is done by pressing Ctrl + X , then Y to confirm, and Enter to exit).

Step 5: Apply the Changes

For the changes to take effect, you either need to restart your terminal or source the profile file by typing source ~/.bash_profile or source ~/.zshrc.

After doing this, when you type python in your terminal, it should now use python3. Keep in mind that this change is specific to your user account on the Mac, and it won’t affect system-level Python configurations.

Didn’t Work? Check the Following:

If setting an alias didn’t work, there might be a few reasons why. Let’s try a different approach:

1. Check Your Shell Again

First, ensure that you edited the correct configuration file for your shell. Run echo $SHELL in the Terminal to confirm your shell. If it’s zsh, the file is ~/.zshrc. For bash, it’s one of ~/.bash_profile, ~/.bashrc, or ~/.profile.

2. Verify Alias Syntax

Make sure that the alias line in your configuration file is exactly as follows:

alias python='python3'

There should be no extra spaces or characters.

3. Recheck Configuration File

Double-check that you saved the changes to the configuration file.

4. Source the Configuration File

Run source ~/.zshrc (for zsh) or source ~/.bash_profile (for bash) to apply the changes immediately. If this step is missed, the alias won’t work until the next time you log in.

5. Restart Terminal

After sourcing the configuration file, close and reopen the Terminal. Sometimes a fresh start is needed.

6. Check for Conflicts

If you have other configuration files (like ~/.bashrc or ~/.profile), they might conflict. Check those files for any existing python aliases or PATH modifications.

7. Check Your PATH

Ensure that the directory containing the python3 executable is in your PATH. Run echo $PATH to see your current PATH.

8. Test the Alias Directly

Try setting the alias directly in the Terminal (not through the file) to see if it works:

alias python='python3'
python --version

If this works, the issue is likely with how the configuration file is being sourced.

9. Use Absolute Path

If the alias still doesn’t work, you can use the absolute path to the python3 executable. Find it using which python3, and then set the alias with that path:

alias python='/absolute/path/to/python3'