Chapter 5. Packaging Python 3 RPMs


You can install Python packages on your system by using the DNF package manager. DNF uses the RPM package format, which offers more downstream control over the software.

Packaging a Python project into an RPM package 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 contents of RPM packages with the rest of the operating system.
  • You can execute tests during the build process.

The packaging format of native Python packages is defined by Python Packaging Authority (PyPA) Specifications. Historically, most Python projects used 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:

  • To package Python software that uses the setup.py file, follow this document.
  • To package more modern packages with pyproject.toml files, see the README file in pyproject-rpm-macros. Note that pyproject-rpm-macros is included in the CodeReady Linux Builder (CRB) repository, which contains unsupported packages, and it can change over time to support newer Python packaging standards.

5.1. A spec file description for an example Python package

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

Note that it is recommended for any RPM package name of a Python library to include the python3- prefix.

See the notes about Python RPM spec files specifics in the following example of the python3-pello package.

An example SPEC file for the pello program written in Python

%global python3_pkgversion 3                                          1

Name:           python-pello                                          2
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:  python%{python3_pkgversion}-devel                     3

# Build dependencies need to be specified manually
BuildRequires:  python%{python3_pkgversion}-setuptools

# Test dependencies need to be specified manually
# Runtime dependencies need to be BuildRequired manually to run tests during build
BuildRequires:  python%{python3_pkgversion}-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 python%{python3_pkgversion}-pello                         4
Summary:        %{summary}

%description -n python%{python3_pkgversion}-pello %_description


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


%build
# The macro only supports projects with setup.py
%py3_build                                                            5


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


%check                                                                6
%pytest


# Note that there is no %%files section for python-pello
%files -n python%{python3_pkgversion}-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
By defining the python3_pkgversion macro, you set which Python version this package will be built for. To build for the default Python version 3.12, remove the line.
2
When packaging a Python project into RPM, always add the python- prefix to the original name of the project. The project name here is Pello and, therefore, the name of the Source RPM (SRPM) is python-pello.
3
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 that you package, for example, python3-setuptools or the runtime and testing dependencies needed to run the tests in the %check section.
4
When choosing a name for the binary RPM (the package that users will be able to install), add a versioned Python prefix. Use the python3- prefix for the default Python 3.12. You can use the %{python3_pkgversion} macro, which evaluates to 3 for the default Python version 3.12 unless you set it to an explicit version, for example, when a later version of Python is available (see footnote 1).
5
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.
Note

Using the setup.py build and setup.py install commands from the setuptools package is deprecated and will be removed in the future major RHEL release. You can use pyproject-rpm-macros instead.

6
The %check section runs the tests of the packaged project. The exact command depends on the project itself, but you can use the %pytest macro to run the pytest command in an RPM-friendly way.

5.2. Common macros for Python 3 RPMs

In a Python RPM spec file, always use the macros for Python 3 RPMs rather than hardcoding their values.

You can redefine which Python 3 version is used in these macros by defining the python3_pkgversion macro on top of your spec file. For more information, see A spec file description for an example Python package. If you define the python3_pkgversion macro, the values of the macros described in the following table will reflect the specified Python 3 version.

Table 5.1. Macros for Python 3 RPMs
MacroNormal DefinitionDescription

%{python3_pkgversion}

3

The Python version that is used by all other macros. Can be redefined to any future Python versions that will be added.

%{python3}

/usr/bin/python3

The Python 3 interpreter.

%{python3_version}

3.12

The major.minor version of the Python 3 interpreter.

%{python3_sitelib}

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

The location where pure-Python modules are installed.

%{python3_sitearch}

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

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

%py3_build

 

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

%py3_install

 

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

%{py3_shebang_flags}

sP

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.

5.3. Using automatically generated dependencies for Python RPMs

You can automatically generate dependencies for Python RPMs by using upstream-provided metadata.

Prerequisites

Procedure

  1. Include one of the following directories in the resulting RPM:

    • .dist-info
    • .egg-info

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

      python3.12dist(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. Example of a generated requirements tag:

      Requires: python3.12dist(requests)
  2. Inspect the generated Requires.
  3. To remove some of the generated Requires, modify the upstream-provided metadata in the %prep section of the spec file.
  4. To disable the automatic requirements generator, include the %{?python_disable_dependency_generator} macro above the main package’s %description declaration.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.