Python Versions
Internal
Overview
This page document how to deal with multiple Python versions on the same system. pyenv
is a tool that deals with that.
To find out the latest production-ready Python version, go to:
This page also provided a recipe on how to check the interpreter version at runtime.
Python Version Management with Brew
brew
provides concurrently maintained releases for Python 3.x, as python@3.x
. Older versions are being gradually deprecated. Python 2 is deprecated since 2019 and it is not served by brew. Multiple Python 3.x releases can be installed on the same system, at the same time. Within any minor release, Python may be upgraded to a newer version at any time. If you require stability of minor and patch version for virtual environments, use pyenv
.
The Python versions are installed in $(brew --prefix)/Cellar
using the following naming scheme:
$(brew --prefix)/Cellar ├─ ... ├─ python@3.8 ├─ python@3.9 ├─ python@3.10 ├─ python@3.11 ├─ python@3.12 ├─ ...
The default brew install path and Cellar
location is /usr/local/Cellar
but may differ depending on the brew
installation. See:
Versioned symlinks for python
, python-config
, pip
are installed in $(brew --prefix python)/bin
and unversioned symlinks are installed in $(brew --prefix python)/libexec/bin
:
$(brew --prefix)/opt ├─ ... ├─ python@3.9 │ ├─ bin │ │ ├─ python3.9 │ │ ├─ pip3.9 │ │ └─ ... │ └─ libexec │ └─ bin │ ├─ python │ ├─ python3 │ ├─ pip │ ├─ pip3 │ └─ ... │ ├─ ... ├─ python@3.11 │ ├─ bin │ │ ├─ python3.11 │ │ ├─ pip3.11 │ │ └─ ... │ └─ libexec │ └─ bin │ ├─ python │ ├─ python3 │ ├─ pip │ ├─ pip3 │ └─ ... ├─ ...
Install a Specific Version
brew install python@3.12
If multiple versions are installed this way, only one is symlinked in /usr/local/bin/python
and /usr/local/bin/python3
(which one?, looks like the newest one)
To use a specific version other than the one symlinked, update the PATH
:
export PATH="$(brew --prefix)/opt/python@3.12/libexec/bin:${PATH}"
Update a Specific Version to a Newer Patch Level
brew upgrade python@3.12
Get More Info about a Specific Python Version
brew info python@3.12
pip Relationship to Python Version
site-packages
is a directory that contains Python modules. brew
creates it here:
$(brew --prefix)/lib ├─ ... ├─ python@3.9 │ └─ site-packages ├─ ... ├─ python@3.11 │ └─ site-packages ├─ ...
The specific version of the pip
that is being used will infer the corresponding location. pip
installations will store the packages under $(brew --prefix)/lib/python@3.x/site-packages
.
Python 3.y also searches for modules in /Library/Python/3.y/site-packages
and ~/Library/Python/3.y/lib/python/site-packages
. brew
’s site-packages
directory is first created (1) once any brew
formulae with Python bindings are installed, or (2) upon brew install python
.
Python Version Management with pyenv
Configure an "Executable" to Run with a Specific Python Version
#!/opt/brew/opt/python@3.9/bin/python3.9
import ...
if __name__ == '__main__':
...
Check the Interpreter Version at Runtime
This is how to check the interpreter version at runtime:
if sys.version_info >= (3, 7):
...