Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 41. 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 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.

41.1. Installing Python 3

By design, you can install RHEL 8 modules in parallel, including the python27, python36, python38, and python39 modules. Note that parallel installation is not supported for multiple streams within a single module.

You can install Python 3.8 and Python 3.9, including packages built for either 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, or python39-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.

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

41.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, and packages for Python 3.9 include the python39- 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

41.3. Installing additional Python 3 tools for developers

Additional Python tools for developers are distributed through the CodeReady Linux Builder repository in the respective python3x-devel module.

The python38-devel module contains the python38-pytest package and its dependencies: the pyparsing, atomicwrites, attrs, packaging, py, more-itertools, pluggy, and wcwidth packages.

The python39-devel module contains the python39-pytest package and its dependencies: the pyparsing, attrs, packaging, py, more-itertools, pluggy, wcwidth, iniconfig, and pybind11 packages. The python39-devel module also contains the python39-debug and python39-Cython packages.

Important

The CodeReady Linux Builder repository and its content is unsupported by Red Hat.

To install packages from the python39-devel module, use the following the procedure.

Procedure

  1. Enable the CodeReady Linux Builder repository:

    # subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
  2. Enable the python39-devel module:

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

    # yum install python39-pytest

To install packages from the python38-devel module, replace python39- with python38- in the commands above.

41.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.

41.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 on 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.

41.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.

Procedure

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

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

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

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

    $ python2
    $ python2 -m cython --help
    $ pip2 install package