5.5. 打包 Python 3 RPM


大多数 Python 项目使用 Setuptools 打包,并在 setup.py 文件中定义软件包信息。有关 Setuptools 打包的详情,请查看 Setuptools 文档

您还可以将 Python 项目打包到 RPM 软件包中,它与 Setuptools 包装相比有以下优点:

  • 有关软件包在其他 RPM 中的依赖软件包(甚至非 Python)
  • 加密签名

    通过加密签名,RPM 软件包的内容可以验证、集成并测试操作系统的其余部分。

5.5.1. Python 软件包的 spec 文件描述

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

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

与非 Python RPM SPEC 文件相比,Python 项目的 RPM SPEC 文件有一些特定信息。最值得注意的是,Python 库的任何 RPM 软件包的名称必须始终包含确定版本的前缀,例如: Python 3.6 的 python 3、Python 3.8 的 python38、Python 3.9 的 python39、Python 3.11 的 python3.11 或 Python 3.12 的 python3.12

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

%global modname detox                                                
1


Name:           python3-detox                                        
2

Version:        0.12
Release:        4%{?dist}
Summary:        Distributing activities of the tox tool
License:        MIT
URL:            https://pypi.io/project/detox
Source0:        https://pypi.io/packages/source/d/%{modname}/%{modname}-%{version}.tar.gz

BuildArch:      noarch

BuildRequires:  python36-devel                                       
3

BuildRequires:  python3-setuptools
BuildRequires:  python36-rpm-macros
BuildRequires:  python3-six
BuildRequires:  python3-tox
BuildRequires:  python3-py
BuildRequires:  python3-eventlet

%?python_enable_dependency_generator                                 
4


%description

Detox is the distributed version of the tox python testing tool. It makes efficient use of multiple CPUs by running all possible activities in parallel.
Detox has the same options and configuration that tox has, so after installation you can run it in the same way and with the same options that you use for tox.

    $ detox

%prep
%autosetup -n %{modname}-%{version}

%build
%py3_build                                                           
5


%install
%py3_install

%check
%{__python3} setup.py test                                           
6


%files -n python3-%{modname}
%doc CHANGELOG
%license LICENSE
%{_bindir}/detox
%{python3_sitelib}/%{modname}/
%{python3_sitelib}/%{modname}-%{version}*

%changelog
...
Copy to Clipboard Toggle word wrap
1
modname 宏包含 Python 项目的名称。在本例中,它是 detox
2
将 Python 项目打包到 RPM 时,需要将 python3 前缀添加到项目的原始名称。这里的原始名称为 detoxRPM 的名称为 python3-detox
3
BuildRequires 指定了构建和测试此软件包所需的软件包。在 BuildRequires 中,始终包括提供构建 Python 软件包所需的工具的项目: python36-develpython3-setuptools。需要 python36-rpm-macros 软件包,以便 /usr/bin/python3 解释器指令的文件会自动改为 /usr/bin/python3.6
4
每个 Python 软件包都需要一些其他软件包才能正常工作。这些软件包还需要在 spec 文件中指定。要指定依赖项,您可以使用 %python_enable_dependency_generator 宏自动使用 setup.py 文件中定义的依赖项。如果软件包的依赖软件包没有使用 Setuptools 指定,请在附加 Requires 指令中指定它们。
5
%py3_build%py3_install 宏会分别运行 setup.py buildsetup.py install 命令,使用附加参数来指定安装位置、要使用的解释器以及其他详情。
6
检查 部分提供了运行正确 Python 版本的宏。%{__python3} 宏包含 Python 3 解释器的路径,如 /usr/bin/python3。我们建议始终使用宏而不是字面上的路径。

5.5.2. Python 3 RPM 的常见宏

spec 文件中,始终使用用于 Python 3 RPM 的 Macros 的 Macros 表中的宏,而不是硬编码其值。

在宏名称中,总是使用 python3python2,而不是未指定版本的 python。在 SPEC 文件的 BuildRequires 部分中,将特定的 Python 3 版本配置为 python36-rpm-macros,python38-rpm-macros,python39-rpm-macros,python3.11-rpm-macros, 或 python3.12-rpm-macros

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

%{__python3}

/usr/bin/python3

Python 3 解释器

%{python3_version}

3.6

Python 3 解释器的完整版本。

%{python3_sitelib}

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

安装纯 Python 模块的位置。

%{python3_sitearch}

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

安装包含特定架构扩展的模块。

%py3_build

 

使用适合系统软件包的参数运行 setup.py build 命令。

%py3_install

 

使用适合系统软件包的参数运行 setup.py install 命令。

5.5.3. 自动为 Python RPM 提供

在打包 Python 项目时,请确保如果这些目录存在,请确保生成的 RPM 中包含以下目录:

  • .dist-info
  • .egg-info
  • .egg-link

从这些目录中,RPM 构建过程会自动生成虚拟 pythonX.Ydist,如 python3.6dist(detox)。这些虚拟提供由 %python_enable_dependency_generator 宏指定的软件包使用。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat