Manage Python Version(s) on MACOS
MacOS comes with Python pre-installed. But it's usually older than the current recent versions and updates are few.
If you are actively developing you likely want to run a more recent version, especially with the performance improvements in 3.11.x.
Install pyenv to Manage Your Python Versions
A convenient way to fix this is to install PyEnv. This library will help you switch between different versions of Python (in case you need to run Python 2.x for some reason, and in anticipation of Python 4.0 coming).
We will assume you have Brew already installed. Run this command:
brew install pyenv
Now you can install the latest version of Python.
Now you just need to run the following command:
pyenv install 3.11.1
Note that you can substitute 3.11.1 for whatever the latest version of Python is.
How to Set Up Your MacOS PATH for pyenv In ZSH or OhMyZSH
You should have upgrade your Mac's default ZSH terminal with OhMyZSH. We will need to edit the .zshrc file:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
Then run:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
How to Set a Version of Python to Global Default (Bash or ZSH)
You can set the latest version of Python to be global, meaning it will be the default version of Python MacOS uses when you run Python applications.
Run this command:
pyenv global 3.11.1
Again, you can replace 3.911.1 with whatever the latest version is.
Now you can verify that this worked by checking the global version of Python:
pyenv versions
You should see this output:
The Final Step: Close Your Terminal and Restart It
Once you've restarted your terminal, you run the python command and you'll launch the new version of Python instead of the old one.
Python is now updated and set as your default.