As I recently set up my new Mac, a friend’s timely advice led me to explore PyEnv, a popular tool for managing multiple Python versions. Having previously relied on Homebrew (brew) to manage my Python versions on my old Mac, I was curious to learn more about PyEnv and decide if I really need it.
Do you really need PyEnv?
Upon reading about it I realized PyEnv does offer some neat things that one can’t get out of brew.
Some top things that PyEnv offers,
- Using exact patch versions of Python, brews always uses the latest
- Being able to pin a specific Python version to a project
- Auto-switching versions when entering project directories
So it’s easy to see why PyEnv is already better, but I realized I had never had the need for any of the above, sure auto-switching versions would be nice to have, but I use virtual environments for all my Python projects and running
source venv/bin/activateis almost habitual now.
My trusty old setup (sticking to it for now)
Always install Python using brew
brew install python@3.11
brew install python@3.12
...Always create virtual environments by explicitly specifying the Python version
python3.11 -m venv venv # This ✅
python3 -m vevn venv # Not this ❌That’s it!
Linking or Unlinking is not a issue
One might think that with brew install they could run into an issue with brew linking Python to the newer version, but this is not the case.
An easy test to test this,
brew install python@3.11
readlink /opt/homebrew/bin/python3
# Ouptut: ../Cellar/python@3.11/3.11.10/bin/python3.11
mkdir dir1 && cd dir1
python3.11 -m venv venv
source /venv/bin/activate
which python
# Output: /Users/me/dir1/venv/bin/python
readlink /Users/me/dir1/venv/bin/python
# Output: python3.11brew install python@3.12
readlink /opt/homebrew/bin/python3
# ../Cellar/python@3.12/3.12.7_1/bin/python3.12
mkdir dir2 && cd dir2
python3.12 -m venv venv
source /venv/bin/activate
which python
# Output: /Users/me/dir2/venv/bin/python
readlink /Users/me/dir2/venv/bin/python
# Output: python3.12
# If you check for the symlink in /dir1/venv,
# it will still point to python3.11 ✅In the above, when python3.12 is installed using brew only the symlink for python3 is updated, the symlink for python3.11 is left as is, hence the projects using the older Python version are left undisturbed.
The Catch
Don’t brew uninstall your Python version if you have projects dependent on it, if you do, you would have to install the same exact patch version to get things back to how they were!
But if don’t have anything that’s offered in a particular patch this is not a problem as well! I would just delete the project’s venv directory and do a clean install with requirements.txt.