gpt4 book ai didi

python - 如何构建符合 PEP384 的 Python 扩展模块并使用正确的 ABI3 标签对其进行打包?

转载 作者:行者123 更新时间:2023-12-02 02:23:43 26 4
gpt4 key购买 nike

mymodule.c 启动如下:

#define PY_SSIZE_T_CLEAN
#define Py_LIMITED_API 0x03070000
#include "Python.h"

它构建时没有错误和警告。然而,生成的文件名为

mymodulename.cpython-37m-x86_64-linux-gnu.so

构建命令:

$ python setup.py build

当我发布时

$ pip wheel .

wheel 中包含的扩展名相同,因此 wheel 也没有 ABI3 标签。

我希望文件名带有 abi3 或类似内容,如 PEP 425 中所述。

到目前为止,我的研究令人沮丧。我查看了 PEP 384 和 425、docs.python.org 上的文档和相关教程,尤其是它的 C/API 和 distutils、PYPA 的文档、setuptools、wheel 和 pip 上的文档——都无济于事。

当前 Python 源代码发行版中包含的文件/modules/xxmodule.c 仅具有历史意义。
我在这里缺少什么?

最佳答案

这取决于后端;并非所有构建后端都支持 PEP 384。

distutils

例如,当使用纯 distutils 时(所以

from distutils.core import setup

setup(...)

), 无法更改扩展后缀;这就是为什么我不得不问这个问题 Change Cython's naming rules for .so files几年前。但是你不应该使用纯 distutils无论如何,所以本节只是为了完整性。

setuptools

如果您使用 setuptools , 你必须通过 py_limited_api=True创建时 Extension对象。示例:

from setuptools import setup, Extension


setup(
...,
ext_modules=[
# this extension will have the SOABI suffix, e.g.
# cpython-37m-darwin or cpython-39-x86_64-linux-gnu etc.
Extension("foo", ["foo.c"]),
# this extension will have the `abi3.so` suffix
Extension("bar", ["bar.c"], py_limited_api=True),
],
)

setuptools_rust

奖励:如果您从 Rust 代码构建扩展模块并且恰好使用 setuptools_rust ,从 v0.11.4 开始,现在也可以构建符合 PEP 384 的扩展:

from setuptools import setup
from setuptools_rust import RustExtension


setup(
...,
rust_extensions=[
RustExtension("foo.rust", py_limited_api=True, features=["pyo3/abi3"]),
],
)

pip wheel : 构建正确的 wheel 标签

When I issue

$ pip wheel .

the extension contained in the wheel is named the same, and consequently the wheel has no ABI3 tag either.

wheel 标签有点不同;它独立于它打包的扩展名称。要设置有限的 ABI,当以通常的方式构建轮子时,会发出

$ python setup.py bdist_wheel --py-limited-api=cp37

生成的轮子将具有名称 <pkg>-<ver>-cp37-abi3-<platform>.whl .

但是,这不适用于 pip wheel因为您不能将选项传递给 bdist_wheel子命令。因此,您可以将其保存在 setup.cfg 中。 :

# setup.cfg
[bdist_wheel]
py_limited_api = cp37

运行时 pip wheel .现在,bdist_wheel将从 setup.cfg 中选取选项并组装正确的车轮名称。

关于python - 如何构建符合 PEP384 的 Python 扩展模块并使用正确的 ABI3 标签对其进行打包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66045571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com