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:
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.
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
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.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'
Save the
~/.bash_profile
or~/.zshrc
file and exit the text editor.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
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.Also, alias the pip3 to pip by doing the below:
find the pip3 path by typing the below command in terminal
which pip3
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'
Save the file and reload the shell using source command like above in step 6.