Files
gitea-pages/admin-guide/configuration/software/python.md

82 lines
3.6 KiB
Markdown

# Python
## Overview
```{note}
This guide only covers RHEL8/9 and newer. Also we will __not__ cover how to use Python 2 any more.
```
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.
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 vs Default Python
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`.
```