1

How do I select the Python version when building a RPM from a .spec file on Fedora 35?

The CentOS/RHEL documentation says

Configure the particular Python 3 version in the BuildRequires of the SPEC file to python36-rpm-macros, python38-rpm-macros, or python39-rpm-macros.

CHAPTER 40. PACKAGING PYTHON 3 RPMS

Fedora only has a python3.9 package and python3-rpm-macros package.

How can I set the Python version? How can I make this selection portable between Fedora, RHEL, and other Enterprise Linux flavors?

Attempt at solution

%define __python3 /usr/bin/python3.9
%{?fedora:BuildRequires: python3.9}
%{?rhel:BuildRequires: python39-devel}

Is this correct?

user7610
  • 25,267
  • 15
  • 124
  • 150
  • 1
    Example CentOS8 : Has {python36-rpm-macros, python38-rpm-macros, python39-rpm-macros}. ..... SPEC file, sketchy examples : %if 0%{?fedora} > 22 → new line = BuildRequires: python3-rpm-macros → new line = %endif ....... and CentOS, RHEL : %if 0%{?rhel} > 7 → new line = BuildRequires: python39-rpm-macros → new line = %endif – Knud Larsen Apr 08 '22 at 13:55
  • 1
    @KnudLarsen I got a comment from a colleague. Python 3.9 on Fedora is mostly intended for Tox testing for developers using Python. It is not intended for RPM package builders. On the other hand, on CentOS, the Pythons there _are_ intended for this use. So that is why CentOS has `python39-rpm-macros` and Fedora does not. – user7610 Apr 08 '22 at 17:55

1 Answers1

0
%global python_minimum_version 3.9.0

%{?fedora:Requires: python3 >= %{python_minimum_version}}
%{?rhel:Requires: python39 >= %{python_minimum_version}}

%{?fedora:BuildRequires: python3-devel >= %{python_minimum_version}}
# yes, I do crazy things in the rpm build, and I need pip
%{?fedora:BuildRequires: python3-pip}
# without wheel the installed files lack `python_qpid_proton-0.37.0.dist-info`
%{?fedora:BuildRequires: python3-wheel}
%{?rhel:BuildRequires: python39-devel >= %{python_minimum_version}}
%{?rhel:BuildRequires: python39-pip}
%{?rhel:BuildRequires: python39-wheel}
%{?rhel:BuildRequires: python39-rpm-macros}

This seems to produce a package that behaves well on both Fedora and CentOS Stream 8.

On Fedora, latest Python is used, and on CentOS I get Python 3.9 and the #! lines in script get rewritten to use that Python 3.9 automatically.

I consider this a success.

user7610
  • 25,267
  • 15
  • 124
  • 150