How to map 'python' to 'python3' on Mac

In this tutorial, we will explore how to configure your machine to use python commands instead of python3.

In this tutorial, we will explore how to configure your machine to use python commands instead of python3.

When you install Python 3 or the latest version of Python on your Mac, it is typically executed using the python3 command. However, if you prefer to use Python 3 while running it with just python commands then this tutorial will show you how.

Before continuing with the tutorial, the article will assume that you have Python installed on your Mac. If you don't have it installed, please refer to the instructions on how to install Python on Mac.

Map '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 configuration file. Here's how:

Step 1: Open Terminal

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

Step 2: Identify your shell

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

Step 3: Edit the configuration file

  1. If you are using bash, you would edit ~/.bash_profile, ~/.bashrc or ~/.profile.
    If you are using zsh, you would edit ~/.zshrc.

Step 4: Add alias

Open the appropriate file in a text editor, such as Nano, by entering 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 changes

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

After doing this, when you type python into your terminal it will use python3. Remember that this change is specific to your user account on the Mac and it will not affect system-level Python configuration.

The above method doesn't work? Try the following methods!

If setting up an alias doesn't work, it could be for a number of reasons. Let's try another approach:

1. Check the shell again

First, make sure you have edited the correct configuration file for your shell. Run echo $SHELL in Terminal to confirm your shell. If it is zsh, the file will be ~/.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 like this:

alias python='python3'

There should be no additional spaces or characters.

3. Check the configuration file again

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

4. Source configuration file

Run source ~/.zshrc (for zsh) or source ~/.bash_profile (for bash) to apply the changes immediately. If this step is skipped, the alias will be inactive until your next login.

5. Restart the terminal

After locating the configuration file, close and reopen Terminal. Sometimes a new start is needed.

6. Check for conflicts

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

7. Check your PATH

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

8. Check alias directly

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

alias python='python3' python --version

If this method works, the problem may lie in how to source the configuration file.

9. Use absolute paths

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

alias python='/absolute/path/to/python3'
Update 02 January 2024
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile