I noticed more and more after hearing Python 2’s end of life date that I wanted everything to be using Python 3. I had been using Python 3 for all of my Python work but I still kept running into myself typing ‘python‘ to load up a quick Python terminal only to find that it was running Python 2.7.x. I kept having to step back and type in ‘python3‘ to get what I wanted. Why they couldn’t of just updated the default python binary from 2 to 3? Well that’s due to the ugly open source nature of Linux distributions and the Python community being unable to work together in cohesion.
Anyway, now I have all of my machines defaulted to Python 3 so whenever I type just ‘python’ it loads up a Python 3 interpreter. The simple steps below will help you default your shell ‘python’ command to use Python 3 as well as leaving Python 2 untouched as it is still a critical dependency to have for Mac and Linux operating systems.
First, you’ll want to know what shell you are using on Linux or Mac before you can adjust this behavior.
If you are not sure what shell you are using you can run this command:
echo $0 #find what shell you are using
This should return a string of something like: ‘bash’ or ‘zsh’, those are arguably the two most popular shells available.
To default Python to Python3 For the Mac find the option that fits your shell:
If you are using the bash shell run this command:
echo "alias python=python3" >> ~/.bashrc && source ~/.bashrc
If you are using the zsh shell, use this command:
echo "alias python=python3" >> ~/.zshrc && source ~/.zshrc
For Linux:
If you are running bash run this command:
echo "alias python=python3" >> ~/.bashrc && source ~/.bashrc
If you are running zsh run this command:
echo "alias python=python3" >> ~/.zshrc && source ~/.zshrc
As you may have noticed the commands are identical for Linux and Mac. This is assuming you have not adjusted the default shell profile locations. After running these commands you should notice whenever you run ‘python’ in your shell it will load Python 3 by default. All new shells you open should also have this behavior.
Example of this in use: