update documentation regarding Python

This commit is contained in:
2024-07-24 13:45:12 +02:00
parent 3bc46cc26b
commit 2a5075ba50
2 changed files with 80 additions and 42 deletions

View File

@@ -1,48 +1,81 @@
# Python
There are several versions of Python available from RedHat. This guide shows how you can select a specific version to be used on your system.
The use of environment like [`venv`](https://docs.python.org/3/library/venv.html) or [Conda](https://docs.conda.io) is recommended if a single user needs one or multiple specific Python environments. But this is not part of this guide.
This guide is only for RHEL8 and newer.
## Python 3
In Hiera you can select the wished version of Python
## Overview
```{note}
This guide only covers RHEL8/9 and newer. Also we will __not__ cover how to use Python 2 any more.
```
base::python::version: '3.11'
```
The `python` as well as the `python3` command will then link to the interpreter of given version. The same is for `pip` and `pip3`.
### RHEL8
On RHEL8 the default Python version is **3.6**. Available are also
- 3.8
- 3.9
- 3.11
- 3.12
There are several versions of Python(3) available from RedHat. This guide shows how one can install specific/multiple versions and how to configure the default Python of a system.
### RHEL9
On RHEL9 the default Python version is **3.9**. Available are also
- 3.11
- 3.12
### Libraries
The packages prefixed with `python3-` are for the default Python version. For newer versions they have a versioned prefix like `python38-` or `python3.11-`. There might not be for all libraries packages for all availabe Python versions. Then you might need to install your library with `pip`.
For package installation (including with `pip`) with Hiera please check out [this guide](packages).
Note that packages installed with `pip` are not updated automatically.
## Python 2
Don't use.
The use of Python environments like [`venv`](https://docs.python.org/3/library/venv.html) or [Conda](https://docs.conda.io) is recommended if a single user needs one or multiple specific Python environments. But this is not part of this guide.
## Platform Python
For system tools, which are shared over many systems where maybe different Python versions are used by default, you may select to use "Platform Python" instead, which is always the default Python version, independed of what has been selected as default Python.
## Platform Python vs Default Python
For this you need to set the shebang of your script to
```
As many system tools are written in Python each RHEL8/9 system comes with a so called Platform Python. This is a _fixed_ Python version than cannot be modified and usually is also not available via the commandline.
Beside that, on each system you can install multiple Python versions. One of this Python version can then be set as the Default Python. This means that this Python version is called if you call `python` and/or `python3` on the commandline/script.
To explicitly call a specific Python version always specify the full version as follows: `python<version>`, e.g. `python3.12`. The same applies to the `pip` command!
### Using the Platform Python
For system tools, that should run consistently on all systems you may decide to use the "Platform Python" instead of the Default Python. For doing so you need to set the shebang of your script to
```bash
#!/usr/libexec/platform-python
```
## Python Versions
On __RHEL8__ the Platform (and usual Default) Python version is __3.6__. As the time of writing following Python versions are additinally available: _3.8_, _3.9_, _3.11_, _3.12_ .
On __RHEL9__ the Platform (and usual Default) Python version is __3.9__. As the time of writing following Python versions are additinally available: _3.11_, _3.12_
## Configuration
Following hiera keys can be used to configure Python on a system.
### Installing and Setting a Default Python
To install a default Python you can use:
```yaml
python::default_version: '3.11'
```
This will make sure that that this Python version is installed on the system and that this Python version is available via the `python` and `python3` command. The same applies for `pip` and `pip3`.
### Installing Additional Python Versions
To install additional Python versions on the system you can use:
```yaml
python::install_versions: ['3.6', '3.12']
```
This will take care that the necessary packages are installed on the system. These versions are then available by calling the version specific Python command `python<version` (e.g. `python3.12`).
### Installing PYPI Packages
You can install PYPI packages for the individual Python versions the same way as installing RPMS on a system (i.e. via the Puppet package module).
This can be done by adding a label after the PYPI package name:
```yaml
base::pkg_group::python_packages:
- 'numpy:pip' # installs package for the systems default python
- 'numpy:pip3.12' # installs numpy via PYPI for `python3.12`
base::package_groups:
- 'python_packages'
```
__IMPORTANT:__ Note that packages installed with `pip` are not updated automatically!
For additional information on how to install packages (including `pip`) please check out [this guide](packages).
```{note}
The packages prefixed with `python3-` are for the default Default Python version. For newer versions RedHat has a versioned prefix like `python38-` or `python3.11-`. There might not be for all libraries packages for all availabe Python versions. Then you might need to install your library with `pip`.
```