第 3 章 打包 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 软件包的内容与操作系统的其余部分。
  • 您可以在构建过程中执行测试。

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

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

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

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

重要

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

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

使用 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 前缀。将 python3- 前缀用于默认的 Python 3.9、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 命令。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat