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

标签 python c setuptools distutils distutils2

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/

相关文章:

python - 距离矩阵的并行构造

c - _setmode 函数导致调试错误

python - 在 setup.cfg 中查找问题 |路径中看不到模块

python - 获取某个字段是否为NULL

Python Numpy 数据类型性能

c - 以下二维数组声明之间有什么区别?

python - 如何从C共享库返回无符号字符数组来调用python函数

python - 使用 py 3.6 Ubuntu 16.04 安装工具在 Anaconda 环境中安装 TensorFlow 1.2 GPU 时出错

python - 如何指定由 setuptools 创建的命令脚本的 shebang 行

python - 多重排列,包括重复