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.

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'
4 ★ | 1 Vote

May be interested

  • How to install Python on Windows, macOS, LinuxHow to install Python on Windows, macOS, Linux
    to get started with python, you first need to install python on the computer you are using, be it windows, macos or linux. below is a guide to installing python on your computer, specific to each operating system.
  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • Multiple choice quiz about Python - Part 4Multiple choice quiz about Python - Part 4
    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
  • How to use Closure in PythonHow to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Functions in PythonFunctions in Python
    what is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
  • How to use GPT-3 with PythonHow to use GPT-3 with Python
    feel free to use cool gpt-3 technology with your own python scripts using openai's handy api. here are details on how to use gpt-3 with python.
  • Multiple choice quiz about Python - Part 7Multiple choice quiz about Python - Part 7
    following the previous test set, part 7 continues with the topic built-in functions in python. let's try with quantrimang to try the 10 questions below.
  • The next () function in PythonThe next () function in Python
    the next () function in python returns the next element in the iterator. you can add a default value to return if iterable is already the last element.
  • Multiple choice quiz about Python - Part 10Multiple choice quiz about Python - Part 10
    following the previous test, part 10 returned with the python function. let's try with quantrimang to try the 10 questions below.
  • Why should you learn Python programming language?Why should you learn Python programming language?
    python is a multi-purpose programming language created in the late 1980s and named after monty python drama group. let's tipsmake.com find out 3 reasons you should learn python programming language in this article!