Python C模块扩展版本不兼容吗? [英] Is Python C module extension version incompatible?

查看:75
本文介绍了Python C模块扩展版本不兼容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 3.6中编译了python c模块扩展.它在Python 3.6环境中运作良好,但在Python 3.7和3.8环境中则无法运作,并且出现错误无法导入名称'cygrpc'.

I compile a python c module extension in Python 3.6. It works well in Python 3.6 environment but does not work in Python 3.7 and 3.8 environments, it gets error cannot import name 'cygrpc'.

我想知道这是预期的行为吗?如果是,原因是什么.

I wondering is this the expected behavior? If yes, what is the reason it.

推荐答案

这取决于扩展的编译方式以及使用的Python C API接口.但是可以, C扩展通常必须针对特定版本的Python进行编译.在不知道如何创建扩展模块的情况下,我认为需要对其进行重新编译.

It depends on how the extension is compiled and what Python C API interfaces it uses. But yes, C extensions generally have to be compiled to target a specific version of Python. In absence of knowledge of how the extension module is created, I would assume it needs to be recompiled.

通常,Python C API会在每个发行版中进行更改.如果该更改破坏了您的扩展程序中的某些内容,那么您自然必须为新的API接口更新扩展程序.但是,即使您所做的更改是 source-compatible (不需要更改源代码即可支持新的Python版本),这并不意味着已编译的二进制文件必然是兼容的.这是 Binary 界面(ABI)发挥作用的地方.

Usually, the Python C API changes every release. If that change breaks something in your extension, naturally, you'll have to update your extension for the new API interfaces. However, even if your changes are source-compatible (no source code changes are needed to support the new Python version) it does not mean that the compiled binaries are necessarily compatible. This is where the Binary interface (ABI) comes into play.

另请参见:稳定的应用程序二进制接口,其中描述了原因背后:

See also: Stable Application Binary Interface which describes the reason behind this:

原因主要是结构定义的演变,其中添加新字段或更改字段类型可能不会破坏API,但会破坏ABI.因此,每个Python版本[...]

The reason is primarily the evolution of struct definitions, where addition of a new field, or changing the type of a field, might not break the API, but can break the ABI. As a consequence, extension modules need to be recompiled for every Python release [...]

还提到了有限的情况,其中二进制文件在各个版本之间都是兼容的:

There is also a mention of a limited case in which binaries are compatible across versions:

从Python 3.2开始,已声明API的子集以确保稳定的ABI.希望使用此API的扩展模块(称为受限API")需要定义Py_LIMITED_API.扩展模块中隐藏了许多解释器详细信息;作为回报,将构建一个无需重新编译即可在任何3.x版本(x> = 2)上运行的模块.

Since Python 3.2, a subset of the API has been declared to guarantee a stable ABI. Extension modules wishing to use this API (called "limited API") need to define Py_LIMITED_API. A number of interpreter details then become hidden from the extension module; in return, a module is built that works on any 3.x version (x>=2) without recompilation.

因此,如果您仅使用稳定API的一部分并定义 Py_LIMITED_API ,则编译后的模块可能会在Python版本之间兼容,而无需重新编译.

So, if you only use parts of the stable API and define Py_LIMITED_API, then the compiled module may be compatible across Python versions without recompilation.

这篇关于Python C模块扩展版本不兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆