Following system colour scheme - Python 增强提案 Selected dark colour scheme - Python 增强提案 Selected light colour scheme - Python 增强提案

Python 增强提案

PEP 739 – build-details.json 1.0 — Python 构建细节的静态描述文件

作者:
Filipe Laíns <lains at python.org>
PEP 代理人:
Paul Moore <p.f.moore at gmail.com>
讨论至:
Discourse 帖子
状态:
已接受
类型:
标准跟踪
主题:
打包
创建日期:
2023年12月19日
Python 版本:
3.14
决议:
Discourse 消息

目录

摘要

本 PEP 引入了 build-details.json,这是一个静态描述文件,其中包含 Python 安装的构建细节。

它包括文件格式 1.0 版本的定义,并定义了此文件的标准位置。

基本原理

在内省 Python 安装时,运行代码通常是不期望的或不可能的。拥有一个静态描述文件可以使 Python 安装的各种构建细节可用,而无需运行解释器。

这对于交叉编译、Python 启动器等用例很有帮助。

范围

build-details.json 是一个安装范围的文件,这意味着它 **只** 能包含在 Python 安装的所有环境中都保持不变的信息。

特定于 Python 环境的信息,例如 site-packages 路径,超出此文件的范围,PEP 作者期望将来会通过另一个 PEP 引入静态环境描述文件。

规范

从 Python 3.14 开始,一个名为 build-details.json 的文件,遵循本 PEP 或未来版本中指定的格式,**必须** 安装在独立于平台的标准库目录 (stdlib,例如 /usr/lib/python3.14/build-details.json) 中,**除非** 由于技术限制而不可行。

注意

除了本 PEP 指定的标准位置外,build-details.json 文件 **可以** 安装在 **其他** 位置,并使用不同的名称。尽管如此,该文件 **应该** 仍然可以在标准位置找到。

实际上,PEP 作者期望未来的 PEP 定义具有更好可发现性的附加安装位置。

格式

格式规范由下面提供的 JSON Schema 定义,此处以人类可读的格式呈现。

$schema https://json-schema.fullstack.org.cn/draft/2020-12/schema
$id https://github.com/python/peps/blob/main/peps/pep-0739/python-build-info-v1.0.schema.json
标题 build-details.json — 包含 Python 安装构建细节的静态描述文件
类型 对象
附加属性 不允许

schema_version

类型 string (常量 — 1.0)
描述 Schema 版本。

这是一个遵循 <MAJOR>.<MINOR> 格式的字符串,其中 <MAJOR><MINOR> 是未填充的数字,代表版本的主次组件。通过将版本字符串解释为十进制数,可以对版本进行算术比较。

对于此规范版本,此值是常量,**必须** 是 1.0

此 schema 的未来版本 **必须** 使用更高的版本号。此 schema 的未来版本 **不得** 使用与其他 schema 版本相同的主版本组件,除非其规范被认为是向后兼容的——它不能以解释数据的语义不同,或在新规范下有效的数据在旧规范下无效的方式更改或扩展当前规范的任何部分,附加属性除外(由 additionalProperties 引起的错误)。

必需

base_prefix

类型 字符串
描述 Python 安装的基础前缀。

要么是绝对路径,要么是相对于此文件所在目录的路径。

示例 /usr, ../.., 等。
必需

base_interpreter

类型 字符串
描述 基础安装的 Python 解释器路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

如果安装提供了解释器可执行文件,则此字段 **必须** 存在。

示例
  • /usr/bin/python
  • bin/python
  • 等等。
必需

platform

类型 字符串
描述 系统平台字符串。

此字段 **应该** 等同于 sysconfig.get_platform()

示例
  • linux-x86_64
  • 等等。
必需

language

类型 对象
描述 包含与 Python 语言规范相关的详细信息的对象。
必需
附加属性 不允许

language.version

类型 字符串
描述 Python 语言版本的字符串表示——一个仅由主版本和次版本组件组成的版本字符串。

此字段 **应该** 等同于 sysconfig.get_python_version()

示例 3.14,等。
必需

language.version_info

类型 对象
描述 格式为 sys.version_info 的对象。

此部分 **应该** 等同于 sys.version_info

示例
  • {'major': 3, 'minor': 14, 'micro': 1, 'releaselevel': 'final', 'serial': 0}
  • 等等。
必需
附加属性 不允许
language.version_info.major
类型 数字
必需
language.version_info.minor
类型 数字
必需
language.version_info.micro
类型 数字
必需
language.version_info.releaselevel
类型 string (枚举 — alpha, beta, candidate, final)
必需
language.version_info.serial
类型 数字
必需

implementation

类型 对象
描述 包含与 Python 实现相关的详细信息的对象。

此部分 **应该** 等同于 sys.implementation。它遵循 PEP 421 中定义的规范,这意味着除了所需的键外,也可以存在实现特定的键,但必须以一个下划线开头。

必需
附加属性 允许

implementation.name

类型 字符串
描述 Python 实现的小写名称。
示例 cpython, pypy, 等。
必需

implementation.version

类型 对象
描述 格式为 sys.version_info 的对象,包含实现版本。
示例
  • {'major': 3, 'minor': 14, 'micro': 1, 'releaselevel': 'final', 'serial': 0}
  • {'major': 7, 'minor': 3, 'micro': 16, 'releaselevel': 'final', 'serial': 0}
  • 等等。
必需
附加属性 不允许
implementation.version.major
类型 数字
必需
implementation.version.minor
类型 数字
必需
implementation.version.micro
类型 数字
必需
implementation.version.releaselevel
类型 string (枚举 — alpha, beta, candidate, final)
必需
implementation.version.serial
类型 数字
必需

abi

类型 对象
描述 包含与 ABI 相关的详细信息的对象。
必需
附加属性 不允许

abi.flags

类型 数组
描述 构建配置标志,用于计算扩展后缀。

这些标志 **必须** 按照它们在扩展后缀中出现的顺序定义。

示例 ['t', 'd'],等。
必需

abi.extension_suffix

类型 字符串
描述 用于针对当前实现版本构建的扩展的后缀。

如果 Python 实现支持扩展,则此字段 **必须** 存在,否则此条目将缺失。

示例
  • .cpython-314-x86_64-linux-gnu.so
  • 等等。
必需

abi.stable_abi_suffix

类型 字符串
描述 用于针对 稳定 ABI 构建的扩展的后缀。

如果 Python 实现具有稳定的 ABI 扩展后缀,则此字段 **必须** 存在,否则此条目将缺失。

示例 .abi3.so,等。
必需

suffixes

类型 对象
描述 按类型分组的有效模块后缀。

如果 Python 安装支持导入外部文件,则此部分 **必须** 存在,并且它 **应该** 等同于 importlib.machinery.*_SUFFIXES 属性。

此外,如果 Python 实现提供的扩展类型超出了 importlib.machinery 模块中列出的类型,它们 **可以** 为其添加一个子部分。

示例
  • {'source': ['.py'], 'bytecode': ['.pyc'], 'optimized_bytecode': ['.pyc'], 'debug_bytecode': ['.pyc'], 'extensions': ['.cpython-313-x86_64-linux-gnu.so', '.abi3.so', '.so']}
  • 等等。
必需
附加属性 允许

libpython

类型 对象
描述 包含与 libpython 库相关的详细信息的对象。

如果 Python 安装提供了 libpython 库,则此部分 **必须** 存在,否则此部分将缺失。

必需
附加属性 不允许

libpython.dynamic

类型 字符串
描述 动态 libpython 库的路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

如果 Python 安装提供了动态 libpython 库,则此字段 **必须** 存在,否则此条目将缺失。

示例
  • /usr/lib/libpython3.14.so.1.0
  • lib/libpython3.14.so.1.0
  • 等等。
必需

libpython.dynamic_stableabi

类型 字符串
描述 用于稳定 ABI 的动态 libpython 库的路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

如果 Python 安装提供了针对稳定 ABI 的动态 libpython 库,则此字段 **必须** 存在,否则此条目将缺失。

如果此键存在,则 dynamic 也 **必须** 设置。

示例
  • /usr/lib/libpython3.so
  • lib/libpython3.so
  • 等等。
必需

libpython.static

类型 字符串
描述 静态 libpython 库的路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

如果 Python 安装提供了静态 libpython 库,则此字段 **必须** 存在,否则此条目将缺失。

示例
  • /usr/lib/python3.14/config-3.14-x86_64-linux-gnu/libpython3.14.a
  • lib/python3.14/config-3.14-x86_64-linux-gnu/libpython3.14.a
  • 等等。
必需

c_api

类型 对象
描述 包含与 Python C API 相关的详细信息的对象。

如果 Python 实现提供了 C API,则此部分 **必须** 存在,否则此部分将缺失。

必需
附加属性 不允许

c_api.headers

类型 字符串
描述 C API 头文件的路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

示例
  • /usr/include/python3.14
  • include/python3.14
  • 等等。
必需

c_api.pkgconfig_path

类型 字符串
描述 pkg-config 定义文件的路径。

要么是绝对路径,要么是相对于 base_prefix 的路径。

如果 Python 实现提供了 pkg-config 定义文件,则此字段 **必须** 存在,否则此部分将缺失。

示例
  • /usr/lib/pkgconfig
  • lib/pkgconfig
  • 等等。
必需

arbitrary_data

类型 对象
描述 包含额外任意数据的对象。

这旨在用作应急方案,以包含此规范未涵盖的任何相关数据。实现可以选择在此部分中提供哪些数据。

必需
附加属性 允许

示例

 1{
 2  "schema_version": "1.0",
 3  "base_prefix": "/usr",
 4  "base_interpreter": "/usr/bin/python",
 5  "platform": "linux-x86_64",
 6  "language": {
 7    "version": "3.14",
 8    "version_info": {
 9      "major": 3,
10      "minor": 14,
11      "micro": 0,
12      "releaselevel": "alpha",
13      "serial": 0
14    }
15  },
16  "implementation": {
17    "name": "cpython",
18    "version": {
19      "major": 3,
20      "minor": 14,
21      "micro": 0,
22      "releaselevel": "alpha",
23      "serial": 0
24    },
25    "hexversion": 51249312,
26    "cache_tag": "cpython-314",
27    "_multiarch": "x86_64-linux-gnu"
28  },
29  "abi": {
30    "flags": ["t", "d"],
31    "extension_suffix": ".cpython-314-x86_64-linux-gnu.so",
32    "stable_abi_suffix": ".abi3.so"
33  },
34  "suffixes": {
35    "source": [".py"],
36    "bytecode": [".pyc"],
37    "optimized_bytecode": [".pyc"],
38    "debug_bytecode": [".pyc"],
39    "extensions": [".cpython-314-x86_64-linux-gnu.so", ".abi3.so", ".so"]
40  },
41  "libpython": {
42    "dynamic": "/usr/lib/libpython3.14.so.1.0",
43    "dynamic_stableabi": "/usr/lib/libpython3.so",
44    "static": "/usr/lib/python3.14/config-3.14-x86_64-linux-gnu/libpython3.14.a",
45    "link_extensions": true
46  },
47  "c_api": {
48    "headers": "/usr/include/python3.14",
49    "pkgconfig_path": "/usr/lib/pkgconfig"
50  }
51}

JSON Schema

  1{
  2  "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema",
  3  "$id": "https://github.com/python/peps/blob/main/peps/pep-0739/python-build-info-v1.0.schema.json",
  4  "type": "object",
  5  "title": "build-details.json — a static description file with build details of Python installations",
  6  "required": [
  7    "schema_version",
  8    "base_prefix",
  9    "platform",
 10    "language",
 11    "implementation"
 12  ],
 13  "additionalProperties": false,
 14  "properties": {
 15    "schema_version": {
 16      "type": "string",
 17      "description": "Schema version.\n\nThis is a string following the format ``<MAJOR>.<MINOR>``, where ``<MAJOR>`` and ``<MINOR>`` are unpaded numbers and represent the **major** and **minor** components of the version. Versions may be arithmetically compared by intrepreting the version string as a decimal number.\n\nFor this specification version, this value is constant and **MUST** be ``1.0``.\n\nFuture versions of this schema **MUST** use a higher version number. Future versions of this schema **MUST NOT** use the same **major** version component as other schema version unless its specification is deemed backwards-compatible with them — it can't change, or extend, any parts of the current specification in such a way as the semantics of the interpreted data differ, or that data valid under the new specification is invalid under the older specification, with the exception of additional properties (errors caused by ``additionalProperties``).",
 18      "const": "1.0"
 19    },
 20    "base_prefix": {
 21      "type": "string",
 22      "description": "Base prefix of the Python installation.\n\nEither an absolute path, or a path relative to directory where this file is contained.",
 23      "examples": [
 24        "/usr",
 25        "../.."
 26      ]
 27    },
 28    "base_interpreter": {
 29        "type": "string",
 30        "description": "The path to the Python interprer of the base installation.\n\nEither an absolute path, or a path relative to ``base_prefix``.\n\nThis field **MUST** be present if the installation provides an interpreter executable.",
 31        "examples": [
 32          "/usr/bin/python",
 33          "bin/python"
 34        ]
 35    },
 36    "platform": {
 37      "type": "string",
 38      "description": "System platform string.\n\nThis field **SHOULD** be equivalent to ``sysconfig.get_platform()``.",
 39      "examples": [
 40        "linux-x86_64"
 41      ]
 42    },
 43    "language": {
 44      "type": "object",
 45      "description": "Object containing details related to the Python language specification.",
 46      "required": [
 47        "version"
 48      ],
 49      "additionalProperties": false,
 50      "properties": {
 51        "version": {
 52          "type": "string",
 53          "description": "String representation the Python language version — a version string consisting only of the *major* and *minor* components.\n\nThis field **SHOULD** be equivalent to ``sysconfig.get_python_version()``.",
 54          "examples": ["3.14"]
 55        },
 56        "version_info": {
 57          "type": "object",
 58          "description": "Object in the format of :py:data:`sys.version_info`.\n\nThis section **SHOULD** be equivalent to :py:data:`sys.version_info`.",
 59          "required": ["major", "minor", "micro", "releaselevel", "serial"],
 60          "additionalProperties": false,
 61          "examples": [
 62            {
 63              "major": 3,
 64              "minor": 14,
 65              "micro": 1,
 66              "releaselevel": "final",
 67              "serial": 0
 68            }
 69          ],
 70          "properties": {
 71            "major": {
 72              "type": "number"
 73            },
 74            "minor": {
 75              "type": "number"
 76            },
 77            "micro": {
 78              "type": "number"
 79            },
 80            "releaselevel": {
 81              "type": "string",
 82              "enum": ["alpha", "beta", "candidate", "final"]
 83            },
 84            "serial": {
 85              "type": "number"
 86            }
 87          }
 88        }
 89      }
 90    },
 91    "implementation": {
 92      "type": "object",
 93      "description": "Object containing details related to Python implementation.\n\nThis section **SHOULD** be equivalent to :py:data:`sys.implementation`. It follows specification defined in PEP 421, meaning that on top of the required keys, implementation-specific keys can also exist, but must be prefixed with an underscore.",
 94      "required": [
 95        "name",
 96        "version",
 97        "hexversion",
 98        "cache_tag"
 99      ],
100      "additionalProperties": true,
101      "properties": {
102        "name": {
103          "type": "string",
104          "description": "Lower-case name of the Python implementation.",
105          "examples": ["cpython", "pypy"]
106        },
107        "version": {
108          "type": "object",
109          "description": "Object in the format of :py:data:`sys.version_info`, containing the implementation version.",
110          "required": ["major", "minor", "micro", "releaselevel", "serial"],
111          "additionalProperties": false,
112          "examples": [
113            {
114              "major": 3,
115              "minor": 14,
116              "micro": 1,
117              "releaselevel": "final",
118              "serial": 0
119            },
120            {
121              "major": 7,
122              "minor": 3,
123              "micro": 16,
124              "releaselevel": "final",
125              "serial": 0
126            }
127          ],
128          "properties": {
129            "major": {
130              "type": "number"
131            },
132            "minor": {
133              "type": "number"
134            },
135            "micro": {
136              "type": "number"
137            },
138            "releaselevel": {
139              "type": "string",
140              "enum": ["alpha", "beta", "candidate", "final"]
141            },
142            "serial": {
143              "type": "number"
144            }
145          }
146        }
147      }
148    },
149    "abi": {
150      "type": "object",
151      "description": "Object containing details related to ABI.",
152      "required": [
153        "flags"
154      ],
155      "additionalProperties": false,
156      "properties": {
157        "flags": {
158          "type": "array",
159          "description": "Build configuration flags, used to calculate the extension suffix.\n\nThe flags **MUST** be defined in the order they appear on the extension suffix.",
160          "additionalProperties": true,
161          "examples": [
162            ["t", "d"]
163          ]
164        },
165        "extension_suffix": {
166          "type": "string",
167          "description": "Suffix used for extensions built against the current implementation version.\n\nThis field **MUST** be present if the Python implementation supports extensions, otherwise this entry will be missing.",
168          "examples": [
169            ".cpython-314-x86_64-linux-gnu.so"
170          ]
171        },
172        "stable_abi_suffix": {
173          "type": "string",
174          "description": "Suffix used for extensions built against the stable ABI.\n\nThis field **MUST** be present if the Python implementation has a stable ABI extension suffix, otherwise this entry will be missing.",
175          "examples": [
176            ".abi3.so"
177          ]
178        }
179      }
180    },
181    "suffixes": {
182      "type": "object",
183      "description": "Valid module suffixes grouped by type.\n\nThis section **MUST** be present if the Python installation supports importing external files, and it **SHOULD** be equivalent to the ``importlib.machinery.*_SUFFIXES`` attributes.\n\nAdditionally, if a Python implementation provides extension kinds other than the ones listed on ``importlib.machinery`` module, they **MAY** add a sub-section for them.",
184      "examples": [
185        {
186          "source": [".py"],
187          "bytecode": [".pyc"],
188          "optimized_bytecode": [".pyc"],
189          "debug_bytecode": [".pyc"],
190          "extensions": [".cpython-313-x86_64-linux-gnu.so", ".abi3.so", ".so"]
191        }
192      ]
193    },
194    "libpython": {
195      "type": "object",
196      "description": "Object containing details related to the ``libpython`` library.\n\nThis section **MUST** by present if Python installation provides a ``libpython`` library, otherwise this section will be missing.",
197      "additionalProperties": false,
198      "properties": {
199        "dynamic": {
200          "type": "string",
201          "description": "The path to the dynamic ``libpython`` library.\n\nEither an absolute path, or a path relative to ``base_prefix``.\n\nThis field **MUST** be present if the Python installation provides a dynamic ``libpython`` library, otherwise this entry will be missing.",
202          "examples": [
203            "/usr/lib/libpython3.14.so.1.0",
204            "lib/libpython3.14.so.1.0"
205          ]
206        },
207        "dynamic_stableabi": {
208          "type": "string",
209          "description": "The path to the dynamic ``libpython`` library for the stable ABI.\n\nEither an absolute path, or a path relative to ``base_prefix``.\n\nThis field **MUST** be present if the Python installation provides a dynamic ``libpython`` library targetting the Stable ABI, otherwise this entry will be missing.\n\nIf this key is present ``dynamic`` **MUST** also be set.",
210          "examples": [
211            "/usr/lib/libpython3.so",
212            "lib/libpython3.so"
213          ]
214        },
215        "static": {
216          "type": "string",
217          "description": "The path to the static ``libpython`` library.\n\nEither an absolute path, or a path relative to ``base_prefix``.\n\nThis field **MUST** be present if the Python installation provides a static ``libpython`` library, otherwise this entry will be missing.",
218          "examples": [
219            "/usr/lib/python3.14/config-3.14-x86_64-linux-gnu/libpython3.14.a",
220            "lib/python3.14/config-3.14-x86_64-linux-gnu/libpython3.14.a"
221          ]
222        },
223        "link_extensions": {
224          "type": "boolean",
225          "description": "Should extensions built against a dynamic ``libpython`` link to it?\n\nThis field **MUST** be present if the Python installation provides a dynamic ``libpython`` library, otherwise this entry will be missing."
226        }
227      }
228    },
229    "c_api": {
230      "type": "object",
231      "description": "Object containing details related to the Python C API.\n\nThis section **MUST** be present if the Python implementation provides a C API, otherwise this section will be missing.",
232      "required": [
233        "headers"
234      ],
235      "additionalProperties": false,
236      "properties": {
237        "headers": {
238          "type": "string",
239          "description": "The path to the C API headers.\n\nEither an absolute path, or a path relative to ``base_prefix``.",
240          "examples": [
241            "/usr/include/python3.14",
242            "include/python3.14"
243          ]
244        },
245        "pkgconfig_path": {
246          "type": "string",
247          "description": "The path to the pkg-config definition files.\n\nEither an absolute path, or a path relative to ``base_prefix``.\n\nThis field **MUST** be present if the Python implementation provides pkg-config definition files, otherwise this section will be missing.",
248          "examples": [
249            "/usr/lib/pkgconfig",
250            "lib/pkgconfig"
251          ]
252        }
253      }
254    },
255    "arbitrary_data": {
256      "type": "object",
257      "description": "Object containing extra arbitrary data.\n\nThis is meant to be used as an escape-hatch, to include any relevant data that is not covered by this specification. Implementations may choose what data to provide in this section.",
258      "additionalProperties": true
259    }
260  }
261}

被拒绝的想法

包含环境特定数据

本 PEP 讨论中的主要请求之一是包含其他类型的信息,例如 site-packages 路径。PEP 作者认为,有关 Python 环境的信息应由单独的文件提供。

在配置文件中包含环境特定数据意味着它将是环境特定的,因此虚拟环境将需要自己的配置文件。这会产生问题,因为虚拟环境在基础 Python 安装更新后仍然存在,可能导致静态配置文件过时,使其数据不可靠,从而失去了其目的。

本 PEP 中部分实现的拟议解决方案是,拥有一个指向基础 Python 安装的 build-details.json 文件,以及一个指向特定环境的 environment.json 文件。

由于 build-details.json 是 Python 发行版的一部分,当基础 Python 安装更新时,build-details.json 也会更新,确保静态描述文件永不过时。


来源:https://github.com/python/peps/blob/main/peps/pep-0739.rst

上次修改:2025-02-07 01:10:57 GMT