Python version handling in 5 easy steps: with pyenv on macOS
Unlike Node, Python is often used for various system level scripts and utilities so its important to keep the system Python stable.
Below is my current macOS system Python version:
❯ python --version
Python 3.9.6
Early in my career I hit problems after changing the system PHP version when I should have been tinkering with the version in MAMP (a macOS local dev environment). Thankfully I'm a little wiser now.
Getting off on the right foot with Python was important and the pyenv version management tool solves this issue.
Below are 5 easy steps to get setup on macOS:
Steps
- Install
pyenv
:
brew install pyenv
- Install the latest Python version via
pyenv
:
pyenv install 3.11.4
- Set the newly installed version as our global default:
pyenv global 3.11.4
- Confirm the setting:
❯ pyenv version
3.11.4 (set by /Users/johnosullivan/.pyenv/version)
- Finally we need to add the following few lines to our
.zshrc
file to tell our shell session to usepyenv
when it encounters any python commands being executed:
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
Now, when we run a python command in our shell session, pyenv
will step in, take our command and run it with the global default version we have specified: 3.11.4.
We can confirm this by simply checking the python version again:
❯ python --version
Python 3.11.4
More detailed information regarding pyenv
can be found in the references section below.