Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 36. Introduction to Python

Python is a high-level programming language that supports multiple programming paradigms, such as object-oriented, imperative, functional, and procedural paradigms. Python has dynamic semantics and can be used for general-purpose programming.

With Red Hat Enterprise Linux, many packages that are installed on the system, such as packages providing system tools, tools for data analysis, or web applications, are written in Python. To use these packages, you must have the python* packages installed.

36.1. Python versions

Two incompatible versions of Python are widely used, Python 2.x and Python 3.x. RHEL 8 provides the following versions of Python.

Table 36.1. Python versions in RHEL 8

VersionPackage to installCommand examplesAvailable sinceLife cycle

Python 3.6

python3

python3, pip3

RHEL 8.0

full RHEL 8

Python 2.7

python2

python2, pip2

RHEL 8.0

shorter

Python 3.8

python38

python3.8, pip3.8

RHEL 8.2

shorter

Python 3.9

python39

python3.9, pip3.9

RHEL 8.4

shorter

Python 3.11

python3.11

python3.11, pip3.11

RHEL 8.8

shorter

For details about the length of support, see Red Hat Enterprise Linux Life Cycle and Red Hat Enterprise Linux Application Streams Life Cycle.

Each of the Python versions up to 3.9 is distributed in a separate module. Python 3.11 is distributed as a suite of non-modular RPM packages, including the python3.11 package.

You can install multiple Python versions in parallel on the same RHEL 8 system.

Important

Always specify the version of Python when installing it, invoking it, or otherwise interacting with it. For example, use python3 instead of python in package and command names. All Python-related commands must also include the version, for example, pip3, pip2, pip3.8, pip3.9, or pip3.11.

The unversioned python command (/usr/bin/python) is not available by default in RHEL 8. You can configure it using the alternatives command; for instructions, see Configuring the unversioned Python.

Any manual changes to /usr/bin/python, except changes made using the alternatives command, might be overwritten upon an update.

As a system administrator, use Python 3 for the following reasons:

  • Python 3 represents the main development direction of the Python project.
  • Support for Python 2 in the upstream community ended in 2020.
  • Popular Python libraries are discontinuing Python 2 support in upstream.
  • Python 2 in Red Hat Enterprise Linux 8 will have a shorter life cycle and aims to facilitate a smoother transition to Python 3 for customers.

For developers, Python 3 has the following advantages over Python 2:

  • Python 3 enables you to write expressive, maintainable, and correct code more easily.
  • Code written in Python 3 will have greater longevity.
  • Python 3 has new features, including asyncio, f-strings, advanced unpacking, keyword-only arguments, and chained exceptions.

However, legacy software might require /usr/bin/python to be configured to Python 2. For this reason, no default python package is distributed with Red Hat Enterprise Linux 8, and you can choose between using Python 2 and 3 as /usr/bin/python, as described in Configuring the unversioned Python.

Important

System tools in Red Hat Enterprise Linux 8 use Python version 3.6 provided by the internal platform-python package. Red Hat advises customers to use the python36 package instead.

36.2. Notable differences between Python versions

Python versions included in RHEL 8 differ in various aspects.

Python bindings

The python38 and python39 modules and the python3.11 package suite do not include the same bindings to system tools (RPM, DNF, SELinux, and others) that are provided for the python36 module. Therefore, use python36 in instances where the greatest compatibility with the base operating system or binary compatibility is necessary. In unique instances where system bindings are necessary together with later versions of various Python modules, use the python36 module in combination with third-party upstream Python modules installed through pip into Python’s venv or virtualenv environments.

Python 3.11 virtual environments must be created using venv instead of virtualenv

The virtualenv utility in RHEL 8, provided by the python3-virtualenv package, is not compatible with Python 3.11. An attempt to create a virtual environment by using virtualenv will fail with the following error message:

$ virtualenv -p python3.11 venv3.11
Running virtualenv with interpreter /usr/bin/python3.11
ERROR: Virtual environments created by virtualenv < 20 are not compatible with Python 3.11.
ERROR: Use python3.11 -m venv instead.

To create Python 3.11 virtual environments, use the python3.11 -m venv command instead, which uses the venv module from the standard library.