If you have recently installed Python3 on your macOS and you still find the default Python version to be the older one (Python 2.x), then this blog post is for you. In this post, we will discuss how to set Python3 as the default Python version on your Mac.
Step 1: Install Python3
If you haven’t already installed Python3, visit the official Python website and download the latest version of Python3. Once it’s downloaded, follow the installation instructions to install Python3 on your machine.
Step 2: Check the Installed Python Versions
To verify that you have both Python 2.x and Python 3.x installed on your machine, open Terminal and type the following commands:
python --version
python3 --version
The output should show the respective versions of Python 2.x and Python 3.x installed on your machine.
Step 3: Update Your Shell Configuration
Before changing the default Python version, we need to modify the shell’s configuration file. For macOS, the default shell is Bash, and its configuration file is .bash_profile or .bashrc. To edit the configuration file, follow these steps:
- Open Terminal and type the following command to open the .bash_profile file in the nano text editor:
nano ~/.bash_profile
If the file doesn’t exist, this command will create a new one.
- Add the following line at the end of the file:
alias python='/usr/local/bin/python3'
This line creates an alias for the Python3 version installed in /usr/local/bin/.
- Save the changes by pressing Ctrl + O and exit the editor by pressing Ctrl + X.
- Now, apply the changes by running the command:
source ~/.bash_profile
Step 4: Verify the Default Python Version
To confirm that Python3 is now the default version, open a new Terminal window and run the following command:
python --version
The output should display the Python 3.x version that you installed earlier.
Conclusion
By following these steps, you now have Python3 as the default Python version on your Mac. This not only allows you to enjoy the latest features and improvements of Python3 but also ensures that your Python code is compatible with the latest libraries and practices in the Python community.