Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Python packaging for RHEL 9 & 10 using pyproject RPM macros

May 7, 2025
Miro Hrončok
Related topics:
LinuxPython
Related products:
Red Hat Enterprise Linux

Share:

    For more than a decade, the setup.py file was the cornerstone of Python packages. When packaging RPM packages for Red Hat Enterprise Linux (RHEL), RPM spec files invoked the setup.py script in %build and %install sections, typically using the %py3_build and %py3_install RPM macros. Every Python project relied on either the standard library's distutils module or the more advanced and widely used setuptools package.

    Python RPM packaging for RHEL 9 and 10 has evolved beyond the traditional setup.py approach. This article explores how the new pyproject RPM macros simplify packaging modern Python projects by supporting diverse build backends and reusing upstream metadata. Whether you're maintaining legacy packages or adopting the latest Python standards, you can use the pyproject RPM macros from the RHEL CRB repository.

    How Python packaging has improved

    Python packaging has undergone a massive overhaul by standardizing a build-system independent format for source trees and storing project metadata in pyproject.toml. The distutils module has been deprecated and removed from the Python standard library. The setup.py script has been deprecated in setuptools. Upstream projects now use various build backends, including flit-core, hatchling, poetry-core, and even the traditional setuptools.

    As a result, it is often not possible to RPM-package upstream Python projects—whether from the Python Package Index (PyPI) or internal package indexes the same way as in RHEL 7 or RHEL 8.

    In Fedora, we created pyproject-rpm-macros, a set of RPM macros designed to package standards-based Python projects into RPM packages. These macros are available in the RHEL 9 and 10 CodeReady Linux Builder (CRB) repository. This allows us to frequently update them to newer versions, often following emerging Python packaging standards—most recently, support for dependency groups in pyproject.toml.

    The pyproject RPM macros are designed with the following principles in mind:

    • Support standards over specific tools.
    • Reuse upstream metadata whenever possible instead of duplicating it in the spec file.

    For example, the old way of RPM-packaging Python invoked setup.py build, which only supported distutils/setuptools-based projects. The pyproject RPM macros instead use a standardized protocol to invoke any standard-compliant Python build backend. This approach remains compatible with traditional projects still using setup.py by falling back to setuptools.

    Another example of metadata reuse is dynamically generating BuildRequires based on upstream metadata instead of listing them manually in the spec file.

    The following is an example spec file.

    Name:           python-example
    Version:        1.2.3
    Release:        1%{?dist}
    Summary:        Example Python library
    License:        MIT
    URL:            https://.com/fedora-python/example
    Source:         %{url}/archive/v%{version}/example-%{version}.tar.gz
    BuildArch:      noarch
    
    %description %_description
    ...
    
    %package -n python3-example
    Summary:        %{summary}
    
    %description -n python3-example
    ...
    
    %prep
    %autosetup -p1 -n example-%{version}
    
    # Dynamically generates BuildRequires, including:
    # - Build backend dependencies (PEP 518)
    # - Runtime dependencies for tests
    # - Dependencies from a dependency group (PEP 735)
    %generate_buildrequires
    %pyproject_buildrequires -g test
    
    # Build the wheel package using the upstream-specified build backend (PEP 517)
    %build
    %pyproject_wheel
    
    # Install the wheel package and save the file list
    %install
    %pyproject_install
    
    # Explicitly list the installed importable module to avoid accidental installations.
    # Use -l to assert a %%license file is found (PEP 639).
    %pyproject_save_files -l example
    
    # Test importability and run pytest
    # There is no standard yet for running tests
    %check
    %pyproject_check_import
    %pytest
    
    # Use the upstream-generated file list in the %%files section
    %files -n python3-example -f %{pyproject_files}
    
    %changelog
    ...
    

    This approach also works seamlessly with alternate Python versions by setting the following:

    %global python3_pkgversion 3.12

    Next steps

    The new pyproject RPM macros makes packaging modern Python projects easier by supporting diverse build backends and reusing upstream metadata. For more details about the macros, refer to the Fedora Python packaging guidelines or the pyproject-rpm-macros README.

    Related Posts

    • Profiling Python Programs

    • Extracting dependencies from Python packages

    • How to self-host a Python package index using Pulp

    Recent Posts

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    • Leveraging Ansible Event-Driven Automation for Automatic CPU Scaling in OpenShift Virtualization

    • Python packaging for RHEL 9 & 10 using pyproject RPM macros

    • Kafka Monthly Digest: April 2025

    What’s up next?

    Learn how large language models (LLMs) are created and use Red Hat Enterprise Linux AI to experiment within an LLM in this hands-on learning path.

    Start the activity
    Red Hat Developers logoLinkedInYouTubeTwitterFacebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dasard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue