Hence, inΒ June 2019, Apple announced it was deprecating the Python 2.7 programming language,Β and in April 2022, they removed support for Python 2.7 on macOS devices running Monterey 12.3 and above.
Now if you were to install the latest macOS, open the terminal to run
python --version
You will get this response:
command not found: python
Not ideal, I know.
Step 1: Install Python via brew#
Pre-requisite: Make sure brew is installed.
Advanced users: You can use pyenv
, to manage different versions, I don’t want an additional delay on the terminal due to it, so I don’t use it.
brew install python
This will install the latest version of Python. Only you can access it via python3
and pip via pip3
β not ideal.
To fix this, run this:
brew info python
It should give you the following info:
Python has been installed as
/opt/homebrew/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/[email protected]/libexec/bin
Based on this info, Python 3.11 is installed on your Mac using Homebrew, and the unversioned symlinks have been set up.
Let’s make Python 3.11 the default version.
Step 2: Open bash or zsh config#
Open the shell profile file in your preferred text editor. For example, you can use the nano editor with the following command:
nano ~/.bash_profile
I use zsh and VS Code, so I ran this:
code ~/.zshrc
Step 3: Add Python to your $PATH#
Add the following lines to the file:
export PATH="/opt/homebrew/opt/[email protected]/libexec/bin:$PATH"
We got this from the brew info python
output. Told ya, that’s useful.
Step 4: Save, and you’re done!#
Save the changes to the file by pressing Control + O, then press Enter. Exit the editor by pressing Control + X.
For me, I just saved it via VS Code. No biggie.
Now reload the shell profile by running the following command:
source ~/.bash_profile
For zsh, you could use this:
source ~/.zshrc
I prefer another approach and have it aliased as reload alias reload="exec $SHELL -l"
β you don’t have to use it.
The command exec $SHELL -l
is used to start a new login shell. Here’s what each part of the command does:
exec
: This command replaces the current process with a new one. In this case, it replaces the current shell process with a new login shell process.
$SHELL
: This environment variable contains the path to the user’s default shell. It typically holds the path to the shell specified in the user’s user account settings.
-l
or --login
: This option passed to the shell command indicates that it should start a login shell. A login shell is a shell that is started after a user logs in to their account. It typically executes certain initialization scripts and sets up the environment according to system-wide and user-specific configurations.
By running exec $SHELL -l
, you are essentially starting a new login shell, which will execute the login shell initialization scripts and set up the environment variables as defined by your system and user settings.
This can be useful if you have made changes to your shell configuration files and want to apply those changes immediately without having to log out and log back in.
## Now you got python
and pip
Yay!!
Verify that Python 3.11 is now the default version by running the following command:
python --version
It should display the version number of Python 3.11.
With these steps, you have made Python 3.11 the default version on your Mac. You can now use python
to refer to Python 3.11 in your terminal.
Thanks