Menu Close

Chapter 3. Packaging Python 3 RPMs

You can install Python packages on your system either from the upstream PyPI repository using the pip installer, or using the DNF package manager. DNF uses the RPM package format, which offers more downstream control over the software.

The packaging format of native Python packages is defined by Python Packaging Authority (PyPA) Specifications. Most Python projects use the distutils or setuptools utilities for packaging, and defined package information in the setup.py file. However, possibilities of creating native Python packages have evolved over time. For more information about emerging packaging standards, see pyproject-rpm-macros.

This chapter describes how to package a Python project that uses setup.py into an RPM package. This approach provides the following advantages compared to native Python packages:

  • Dependencies on Python and non-Python packages are possible and strictly enforced by the DNF package manager.
  • You can cryptographically sign the packages. With cryptographic signing, you can verify, integrate, and test content of RPM packages with the rest of the operating system.
  • You can execute tests during the build process.

3.1. SPEC file description for a Python package

A SPEC file contains instructions that the rpmbuild utility uses to build an RPM. The instructions are included in a series of sections. A SPEC file has two main parts in which the sections are defined:

  • Preamble (contains a series of metadata items that are used in the Body)
  • Body (contains the main part of the instructions)

An RPM SPEC file for Python projects has some specifics compared to non-Python RPM SPEC files.

Important

A name of any RPM package of a Python library must always include the python3- prefix.

Other specifics are shown in the following SPEC file example for the python3-pello package. For description of such specifics, see the notes below the example.

Name:           python-pello                                          1
Version:        1.0.2
Release:        1%{?dist}
Summary:        Example Python library

License:        MIT
URL:            https://github.com/fedora-python/Pello
Source:         %{url}/archive/v%{version}/Pello-%{version}.tar.gz

BuildArch:      noarch
BuildRequires:  python3-devel                                         2

# Build dependencies needed to be specified manually
BuildRequires:  python3-setuptools

# Test dependencies needed to be specified manually
# Also runtime dependencies need to be BuildRequired manually to run tests during build
BuildRequires:  python3-pytest >= 3


%global _description %{expand:
Pello is an example package with an executable that prints Hello World! on the command line.}

%description %_description

%package -n python3-pello                                             3
Summary:        %{summary}

%description -n python3-pello %_description


%prep
%autosetup -p1 -n Pello-%{version}


%build
# The macro only supported projects with setup.py
%py3_build                                                            4


%install
# The macro only supported projects with setup.py
%py3_install


%check                                                                5
%{pytest}


# Note that there is no %%files section for the unversioned python module
%files -n python3-pello
%doc README.md
%license LICENSE.txt
%{_bindir}/pello_greeting

# The library files needed to be listed manually
%{python3_sitelib}/pello/

# The metadata files needed to be listed manually
%{python3_sitelib}/Pello-*.egg-info/
1
When packaging a Python project into RPM, always add the python- prefix to the original name of the project. The original name here is pello and thus the name of the Source RPM (SRPM) is python-pello.
2
BuildRequires specifies what packages are required to build and test this package. In BuildRequires, always include items providing tools necessary for building Python packages: python3-devel and the relevant projects needed by the specific software you package, for example python3-setuptools or the runtime and testing dependencies needed to run the tests in the %check section.
3
When choosing a name for the binary RPM (the package that users will be able to install), add a versioned Python prefix, which is currently python3-. Therefore, the resulting binary RPM will be named python3-pello.
4
The %py3_build and %py3_install macros run the setup.py build and setup.py install commands, respectively, with additional arguments to specify installation locations, the interpreter to use, and other details.
5
The %check section should run the tests of the packaged project. The exact command depends very much on the project itself, but it is possible to use the %pytest macro to run the pytest command in an RPM-friendly way. The %{python3} macro contains a path for the Python 3 interpreter, that is, /usr/bin/python3. We recommend always using the macro rather than a literal path.

3.2. Common macros for Python 3 RPMs

In a SPEC file, always use the macros that are described in the following Macros for Python 3 RPMs table rather than hardcoding their values.

Table 3.1. Macros for Python 3 RPMs

MacroNormal DefinitionDescription

%{python3}

/usr/bin/python3

The Python 3 interpreter

%{python3_version}

3.9

The major.minor version of the Python 3 interpreter

%{python3_sitelib}

/usr/lib/python3.9/site-packages

The location where pure-Python modules are installed

%{python3_sitearch}

/usr/lib64/python3.9/site-packages

The location where modules containing architecture-specific extension modules are installed

%py3_build

 

Runs the setup.py build command with arguments suitable for an RPM package

%py3_install

 

Runs the setup.py install command with arguments suitable for an RPM package

%{py3_shebang_flags}

s

The default set of flags for the Python interpreter directives macro, %py3_shebang_fix

%py3_shebang_fix

 

Changes Python interpreter directives to #! %{python3}, preserves any existing flags (if found), and adds flags defined in the %{py3_shebang_flags} macro

3.3. Using automatically generated dependencies for Python RPMs

The following procedure describes how to use automatically generated dependencies when packaging a Python project as an RPM.

Prerequisites

Procedure

  1. Make sure that one of the following directories containing upstream-provided metadata is included in the resulting RPM:

    • .dist-info
    • .egg-info

      The RPM build process automatically generates virtual pythonX.Ydist provides from these directories, for example:

      python3.9dist(pello)

      The Python dependency generator then reads the upstream metadata and generates runtime requirements for each RPM package using the generated pythonX.Ydist virtual provides. For example, a generated requirements tag might look as follows:

      Requires: python3.9dist(requests)
  2. Inspect the generated requires.
  3. To remove some of the generated requires, use one of the following approaches:

    1. Modify the upstream-provided metadata in the %prep section of the SPEC file.
    2. Use automatic filtering of dependencies described in the upstream documentation.
  4. To disable the automatic dependency generator, include the %{?python_disable_dependency_generator} macro above the main package’s %description declaration.