6.5. 打包 Python 3 RPM


您可以使用 pip 安装程序,或使用 DNF 软件包管理器在系统中安装 Python 软件包。DNF 使用 RPM 软件包格式,它提供了更好的软件下游控制。

原生 Python 软件包的打包格式由 Python Packaging Authority (PyPA) 规范定义。大多数 Python 项目使用 distutilssetuptools 实用程序进行打包,并在 setup.py 文件中定义的软件包信息。然而,创建原生 Python 软件包可能会随着时间而有新的演变。有关新兴打包标准的更多信息,请参阅 pyproject-rpm-macros

本章论述了如何将 setup.py 的 Python 项目打包到一个 RPM 软件包中。与原生 Python 软件包相比,此方法提供以下优点:

  • 可以对 Python 和非 Python 软件包的依赖项,并严格由 DNF 软件包管理器强制执行。
  • 您可以用加密的方式为软件包签名。使用加密签名,您可以验证、集成和测试 RPM 软件包的内容与操作系统的其余部分。
  • 您可以在构建过程中执行测试。

6.5.1. SPEC 文件是 Python 软件包的描述

SPEC 文件包含 rpmbuild 实用程序用于构建 RPM 的指令。这些指令包含在不同的部分。SPEC 文件有两个主要部分用于定义构建指令:

  • Preamble(包含一系列在 Body 中使用的元数据项)
  • Body(包含指令的主要部分)

与非 Python RPM SPEC 文件相比,Python 项目的 RPM SPEC 文件有一些特定信息。

重要

Python 库的任何 RPM 软件包的名称必须始终包含 python3-python3.11-python3.12- 前缀。

以下 SPEC 文件示例中显示了 python3*-pello 软件包的其他具体信息。有关此类特定描述,请查看示例中的备注。

使用 Python 编写的程序的 spec 文件示例

%global python3_pkgversion 3.12                                       
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 needed to be specified manually
BuildRequires:  python%{python3_pkgversion}-setuptools

# Test dependencies needed to be specified manually
# Also 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 supported projects with setup.py
%py3_build                                                            
5



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


%check                                                                
6

%{pytest}


# Note that there is no %%files section for the unversioned python module
%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/

%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 1.0.2-1
- First pello package
Copy to Clipboard Toggle word wrap

1
通过定义 python3_pkgversion 宏,您可以设置将为哪个 Python 版本构建此软件包。要为默认的 Python 版本 3.9 构建,请将宏设置为默认值 3 或完全删除行。
2
将 Python 项目打包到 RPM 中时,需要将 python- 前缀添加到项目的原始名称中。这里原始名称为 pello,因此源 RPM (SRPM)的名称是 python-pello
3
BuildRequires 指令指定需要哪些软件包来构建和测试此软件包。在 BuildRequires 中,始终包括提供构建 Python 软件包所需的工具: python3-devel (或 python3.11-develpython3.12-devel)以及您软件包的特定软件所需的相关软件包,如 python3-setuptools (或 python3.11-setuptoolspython3.12-setuptools)或运行 %check 部分中测试所需的运行时和测试依赖项。
4
当为二进制 RPM 选择名称(用户必须安装的软件包)时,请添加版本化的 Python 前缀。对默认的 Python 3.9 使用 python3- 前缀,对Python 3.11 使用 python3.11- 前缀或对 Python 3.12 使用 python3.12- 前缀。您可以使用 %{python3_pkgversion} 宏,它针对默认的 Python 版本 3.9 评估为 3,除非您将其设置为显式版本,例如 3.11 (请参阅脚注 1)。
5
%py3_build%py3_install 宏会分别运行 setup.py buildsetup.py install 命令,使用附加参数来指定安装位置、要使用的解释器以及其他详情。
6
%check 部分应该运行打包项目的测试。确切的命令取决于项目本身,但可以使用 %pytest 宏,以 RPM 友好的方式运行 pytest 命令。

6.5.2. Python 3 RPM 的常见宏

在 SPEC 文件中,使用用于 Python 3 RPM 的宏表中的内容来使用宏而不是使用硬编码。您可以通过在 SPEC 文件之上定义 python3_pkgversion 宏来重新定义在这些宏中使用哪个 Python 3 版本(请参阅 第 6.5.1 节 “SPEC 文件是 Python 软件包的描述”)。如果您定义了 python3_pkgversion 宏,则下表中描述的宏的值将反映指定的 Python 3 版本。

Expand
表 6.3. Python 3 RPM 宏
Macro常规定义描述

%{python3_pkgversion}

3

所有其他宏使用的 Python 版本。可以重新定义为 3.11 ,以使用 Python 3.11,或重新定义为 3.12 ,以使用 Python 3.12

%{python3}

/usr/bin/python3

Python 3 解释器

%{python3_version}

3.9

Python 3 解释器的 major.minor 版本

%{python3_sitelib}

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

安装纯 Python 模块的位置

%{python3_sitearch}

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

安装包含特定于架构扩展模块的模块的位置

%py3_build

 

使用适用于 RPM 软件包的参数运行 setup.py build 命令

%py3_install

 

使用适用于 RPM 软件包的参数运行 setup.py install 命令

%{py3_shebang_flags}

s

Python 解释器指令宏的默认标记集,%py3_shebang_fix

%py3_shebang_fix

 

将 Python 解释器指令改为 #! %{python3},保留任何现有标志(如果找到),并添加在 %{py3_shebang_flags} 宏中定义的标记

6.5.3. 为 Python RPM 使用自动生成的依赖项

以下流程描述了如何在将 Python 项目打包为 RPM 时使用自动生成的依赖项。

先决条件

流程

  1. 确保以下包含上游提供元数据的目录之一包含在生成的 RPM 中:

    • .dist-info
    • .egg-info

      RPM 构建过程会自动从这些目录中生成虚拟 pythonX.Ydist,例如:

      python3.9dist(pello)
      Copy to Clipboard Toggle word wrap

      然后,Python 依赖项生成器读取上游元数据,并使用生成的 pythonX.Ydist 虚拟提供为每个 RPM 软件包生成运行时要求。例如,生成的要求标签可能如下所示:

      Requires: python3.9dist(requests)
      Copy to Clipboard Toggle word wrap
  2. 检查生成的要求。
  3. 要删除其中的一些生成的需要,请使用以下方法之一:

    1. 在 SPEC 文件的 %prep 部分中修改上游提供的元数据。
    2. 使用上游文档中描述的依赖项自动过滤。
  4. 要禁用自动依赖项生成器,请在主软件包的 %description 声明中包含 %{?python_disable_dependency_generator} 宏。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat