Python Versions: Difference between revisions
(9 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[brew]] | * [[brew]] | ||
* [[Python Engineering#Subjects|Python Engineering]] | * [[Python Engineering#Subjects|Python Engineering]] | ||
* [[Python Language#Python_Version|Python Language]] | |||
=Overview= | =Overview= | ||
This page document how to deal with multiple Python versions on the same system. <code>[[pyenv|pyenv]]</code> is a tool that deals with that. | This page document how to deal with multiple Python versions on the same system. <code>[[pyenv|pyenv]]</code> is a tool that deals with that. | ||
To find out the latest production-ready Python version, go to: {{External|https://devguide.python.org/versions/}} | |||
This page also provided a recipe on how to [[#Check_the_Interpreter_Version_at_Runtime|check the interpreter version at runtime]]. | |||
=Python Version Management with Brew= | =Python Version Management with Brew= | ||
Line 76: | Line 81: | ||
==Get More Info about a Specific Python Version== | ==Get More Info about a Specific Python Version== | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
brew info python@3. | brew info python@3.12 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==<tt>pip</tt> | ==<span id='pip_relationship_to_Python_Version'></span><tt>pip</tt> Relationship to Python Version== | ||
<code>site-packages</code> is a directory that contains Python modules. <code>brew</code> creates it here: | <code>site-packages</code> is a directory that contains Python modules. <code>brew</code> creates it here: | ||
Line 92: | Line 97: | ||
├─ ... | ├─ ... | ||
</font> | </font> | ||
The specific version of the <code>pip</code> that is being used will infer the corresponding location. | The specific version of the <code>pip</code> that is being used will infer the corresponding location. <code>pip</code> installations will store the packages under <code>$(brew --prefix)/lib/python@3.x/site-packages</code>. | ||
Python 3.y also searches for modules in <code>/Library/Python/3.y/site-packages</code> and <code>~/Library/Python/3.y/lib/python/site-packages</code>. <code>brew</code>’s <code>site-packages</code> directory is first created (1) once any <code>brew</code> formulae with Python bindings are installed, or (2) upon <code>brew install python</code>. | Python 3.y also searches for modules in <code>/Library/Python/3.y/site-packages</code> and <code>~/Library/Python/3.y/lib/python/site-packages</code>. <code>brew</code>’s <code>site-packages</code> directory is first created (1) once any <code>brew</code> formulae with Python bindings are installed, or (2) upon <code>brew install python</code>. | ||
=Python Version Management with <tt>pyenv</tt>= | =Python Version Management with <tt>pyenv</tt>= | ||
{{Internal| | {{Internal|Pyenv_Concepts#Version_Selection|<tt>pyenv</tt> Version Selection}} | ||
=Configure an "Executable" to Run with a Specific Python Version= | |||
<syntaxhighlight lang='py'> | |||
#!/opt/brew/opt/python@3.9/bin/python3.9 | |||
import ... | |||
if __name__ == '__main__': | |||
... | |||
</syntaxhighlight> | |||
=Check the Interpreter Version at Runtime= | |||
This is how to check the interpreter version at runtime: {{Internal|Python_Module_sys#sys.version_info|<tt>sys.version_info</tt>}} | |||
<syntaxhighlight lang='py'> | |||
if sys.version_info >= (3, 7): | |||
... | |||
</syntaxhighlight> |
Latest revision as of 22:40, 5 April 2024
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):
...