Python Versions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[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=
{{External|https://docs.brew.sh/Homebrew-and-Python}}
<code>brew</code> provides concurrently maintained releases for Python 3.x, as <code>python@3.x</code>. 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 <code>[[pyenv|pyenv]]</code>.


<code>brew</code> install different Python major and minor versions in <code>Cellar</code> using the following naming scheme:
The Python versions are installed in <code>$(brew --prefix)/Cellar</code> using the following naming scheme:


<font size=-2>
<font size=-2>
   Cellar
   $(brew --prefix)/Cellar
   ├─ ...
   ├─ ...
   ├─ python@3.8
   ├─ python@3.8
Line 20: Line 29:
</font>
</font>


The default <code>Cellar</code> location is <code>/usr/local/Cellar</code> but may differ depending on the <code>brew</code> instance. See: {{Internal|Brew_Concepts#Cellar|<tt>brew</tt> Concepts &#124; Cellar}}
The default brew install path and <code>Cellar</code> location is <code>/usr/local/Cellar</code> but may differ depending on the <code>brew</code> installation. See: {{Internal|Brew_Concepts#Install_Path|<tt>brew</tt> Install Path}}


Versioned symlinks for <code>python</code>, <code>python-config</code>, <code>pip</code> are installed in <code>$(brew --prefix python)/bin</code> and unversioned symlinks are installed in <code>$(brew --prefix python)/libexec/bin</code>:
<font size=-2>
  $(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
  │          └─ ...
  ├─ ...
</font>
==Install a Specific Version==
==Install a Specific Version==
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
brew install python@3.12
brew install python@3.12
</syntaxhighlight>
If multiple versions are installed this way, only one is symlinked in <code>/usr/local/bin/python</code> and <code>/usr/local/bin/python3</code> <font color=darkkhaki>(which one?, looks like the newest one)</font>
To use a specific version other than the one symlinked, update the <code>PATH</code>:
<syntaxhighlight lang='bash'>
export PATH="$(brew --prefix)/opt/python@3.12/libexec/bin:${PATH}"
</syntaxhighlight>
</syntaxhighlight>


Line 30: Line 78:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
brew upgrade python@3.12
brew upgrade python@3.12
</syntaxhighlight>
==Get More Info about a Specific Python Version==
<syntaxhighlight lang='bash'>
brew info python@3.12
</syntaxhighlight>
</syntaxhighlight>


=<tt>pip</tt> relationship to Python Version=
==<span id='pip_relationship_to_Python_Version'></span><tt>pip</tt> Relationship to Python Version==


<code>pip</code> installs the packages in a directory
<code>site-packages</code> is a directory that contains Python modules. <code>brew</code> creates it here:
<font size=-2>
  $(brew --prefix)/lib
  ├─ ...
  ├─ python@3.9
  │  └─ site-packages
  ├─ ...
  ├─ python@3.11
  │  └─ site-packages
  ├─ ...
</font>
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 Version Management with <tt>pyenv</tt>=
{{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:

https://devguide.python.org/versions/

This page also provided a recipe on how to check the interpreter version at runtime.

Python Version Management with Brew

https://docs.brew.sh/Homebrew-and-Python

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:

brew Install Path

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

pyenv Version Selection

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:

sys.version_info
if sys.version_info >= (3, 7):
  ...