Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 37. Installing and using Python

In Red Hat Enterprise Linux 8, Python 3 is distributed in versions 3.6, 3.8, and 3.9, provided by the python36, python38, and python39 modules, and the python3.11 package suite in the AppStream repository.

Warning

Using the unversioned python command to install or run Python does not work by default due to ambiguity. Always specify the version of Python, or configure the system default version by using the alternatives command.

37.1. Installing Python 3

By design, you can install RHEL 8 modules in parallel, including the python27, python36, python38, and python39 modules, and the python3.11 package suite.

You can install Python 3.8, Python 3.9, and Python 3.11, including packages built for each version, in parallel with Python 3.6 on the same system, with the exception of the mod_wsgi module. Due to a limitation of the Apache HTTP Server, only one of the python3-mod_wsgi, python38-mod_wsgi, python39-mod_wsgi, or python3.11-mod_wsgi packages can be installed on a system.

Procedure

  • To install Python 3.6 from the python36 module, use:

    # yum install python3

    The python36:3.6 module stream is enabled automatically.

  • To install Python 3.8 from the python38 module, use:

    # yum install python38

    The python38:3.8 module stream is enabled automatically.

  • To install Python 3.9 from the python39 module, use:

    # yum install python39

    The python39:3.9 module stream is enabled automatically.

  • To install Python 3.11 from the python3.11 RPM package, use:

    # yum install python3.11

Verification steps

  • To verify the Python version installed on your system, use the --version option with the python command specific for your required version of Python.

    • For Python 3.6:

      $ python3 --version
    • For Python 3.8:

      $ python3.8 --version
    • For Python 3.9:

      $ python3.9 --version
    • For Python 3.11:

      $ python3.11 --version

37.2. Installing additional Python 3 packages

Packages with add-on modules for Python 3.6 generally use the python3- prefix, packages for Python 3.8 include the python38- prefix, packages for Python 3.9 include the python39- prefix, and packages for Python 3.11 include the python3.11- prefix. Always include the prefix when installing additional Python packages, as shown in the examples below.

Procedure

  • To install the Requests module for Python 3.6, use:

    # yum install python3-requests
  • To install the Cython extension to Python 3.8, use:

    # yum install python38-Cython
  • To install the pip package installer from Python 3.9, use:

    # yum install python39-pip
  • To install the pip package installer from Python 3.11, use:

    # yum install python3.11-pip

37.3. Installing additional Python 3 tools for developers

Additional Python tools for developers are distributed mostly through the CodeReady Linux Builder (CRB) repository in the respective python38-devel or python39-devel module, or the python3.11-* packages.

The python3-pytest package (for Python 3.6) and its dependencies are available in the AppStream repository.

The CRB repository provides:

  • The python38-devel module, which contains the python38-pytest package and its dependencies.
  • The python39-devel module, which contains the python39-pytest package and its dependencies, and the python39-debug and python39-Cython packages.
  • The python3.11-* packages, which include:

    • python3.11-pytest and its dependencies
    • python3.11-idle
    • python3.11-debug
    • python3.11-Cython
Important

The content in the CodeReady Linux Builder repository is unsupported by Red Hat.

Note

Not all upstream Python-related packages are available in RHEL.

To install the python3*-pytest package, use the following procedure.

Procedure

  1. For Python 3.8 and later, enable the CodeReady Linux Builder repository:

    # subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
  2. For Python 3.8 or 3.9, enable the respective python3*-devel module, for example:

    # yum module enable python39-devel
  3. Install the python3*-pytest package:

    • For Python 3.6:

      # yum install python3-pytest
    • For Python 3.8:

      # yum install python38-pytest
    • For Python 3.9:

      # yum install python39-pytest
    • For Python 3.11:

      # yum install python3.11-pytest

37.4. Installing Python 2

Some applications and scripts have not yet been fully ported to Python 3 and require Python 2 to run. Red Hat Enterprise Linux 8 allows parallel installation of Python 3 and Python 2. If you need the Python 2 functionality, install the python27 module, which is available in the AppStream repository.

Warning

Note that Python 3 is the main development direction of the Python project. Support for Python 2 is being phased out. The python27 module has a shorter support period than Red Hat Enterprise Linux 8.

Procedure

  • To install Python 2.7 from the python27 module, use:

    # yum install python2

    The python27:2.7 module stream is enabled automatically.

Packages with add-on modules for Python 2 generally use the python2- prefix. Always include the prefix when installing additional Python packages, as shown in the examples below.

  • To install the Requests module for Python 2, use:

    # yum install python2-requests
  • To install the Cython extension to Python 2, use:

    # yum install python2-Cython

Verification steps

  • To verify the Python version installed on your system, use:

    $ python2 --version
Note

By design, you can install RHEL 8 modules in parallel, including the python27, python36, python38, and python39 modules.

37.5. Migrating from Python 2 to Python 3

As a developer, you may want to migrate your former code that is written in Python 2 to Python 3.

For more information about how to migrate large code bases to Python 3, see The Conservative Python 3 Porting Guide.

Note that after this migration, the original Python 2 code becomes interpretable by the Python 3 interpreter and stays interpretable for the Python 2 interpreter as well.

37.6. Using Python

When running the Python interpreter or Python-related commands, always specify the version.

Prerequisites

  • Ensure that the required version of Python is installed.
  • If you want to download and install third-party applications for Python 3.11, install the python3.11-pip package.

Procedure

  • To run the Python 3.6 interpreter or related commands, use, for example:

    $ python3
    $ python3 -m venv --help
    $ python3 -m pip install package
    $ pip3 install package
  • To run the Python 3.8 interpreter or related commands, use, for example:

    $ python3.8
    $ python3.8 -m venv --help
    $ python3.8 -m pip install package
    $ pip3.8 install package
  • To run the Python 3.9 interpreter or related commands, use, for example:

    $ python3.9
    $ python3.9 -m venv --help
    $ python3.9 -m pip install package
    $ pip3.9 install package
  • To run the Python 3.11 interpreter or related commands, use, for example:

    $ python3.11
    $ python3.11 -m venv --help
    $ python3.11 -m pip install package
    $ pip3.11 install package
  • To run the Python 2 interpreter or related commands, use, for example:

    $ python2
    $ python2 -m pip install package
    $ pip2 install package