How to change the alias for the python command to point to Python 3 on a Mac

After installing Python3 in Mac OS, python can be used using the python3 command. You need to type python3 always instead of just typing python. you can follow these steps to change this:

  1. Open a terminal window and type the following command:

     which python3
    

This will display the path to the Python 3 executable on your system. Make note of this path, as you will need it in the next step.

  1. Open the shell according to your OS. latest macOS uses zsh shell but older versions use bash shell. You can find the appropriate shell for your os by typing the below command in the terminal.

     echo $0
    
  2. If your os is using bash shell then you need to open the file ~/.bash_profile file in a text editor or by using the nano command. For zsh shell you need to open ~/.zshrc file. This file is located in your home directory and controls the environment and settings for your terminal sessions.

  3. Add the following line to the ~/.bash_profile or ~/.zshrc file according to your os, replacing /path/to/python3 with the path to the Python 3 executable that you obtained in step 1:

     alias python='/path/to/python3'
    
  1. Save the ~/.bash_profile or ~/.zshrc file and exit the text editor.

  2. Run the following command to reload the ~/.bash_profile or ~/.zshrc file and apply the changes: Replace ~/.bash_profile with ~/.zshrc if your os is using zsh shell.

     source ~/.bash_profile
    
  3. After following these steps, the python command will now be an alias for the Python 3 executable. To switch back to using Python 2 as the default Python version, you can simply remove the alias by deleting the line that you added to the ~/.bash_profile file and reloading the file as described above.

  4. Also, alias the pip3 to pip by doing the below:

    1. find the pip3 path by typing the below command in terminal

       which pip3
      
    2. Edit the same file you edit above in step 4. Add the below command in the file by replacing the path with the path you obtain from the above command

       alias pip='/path/to/pip3'
      
    3. Save the file and reload the shell using source command like above in step 6.