为什么Cython编译器生成带有后缀'cpython-35m-x86_64-linux-gnu.so'的so [英] why Cython compiler generates a so with suffix 'cpython-35m-x86_64-linux-gnu.so'
问题描述
#setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("module_name", ["xxxx.pyx", './yyyyy.c'],
language='c',
extra_compile_args = ['-std=gnu11'],
extra_link_args=['-std=gnu11']
)]
)
$ python --version
Python 3.5.1
$ python -c "import cython; print(cython.__version__);"
0.25.2
$ python setup.py build_ext -i
问题>为什么Cython生成模块名称为 module_name.cpython-35m-x86_64-linux-gnu.so
而不是 module_name.so
?
Question> Why Cython generates the module name as module_name.cpython-35m-x86_64-linux-gnu.so
instead of module_name.so
?
谢谢
推荐答案
完整说明 。简而言之:这是从Python 3.2开始应用的更改。故意将版本标识符添加到文件名中;具有此版本标识符的文件名优先于不具有此版本标识符的文件名(但是,如果没有其他选项可导入,则将使用不具有版本标识符 的纯.so或.pyd文件)。
A full description is available in PEP 3149. In short: this is a change that applies from Python 3.2 onwards. A version identifier is deliberately added to the filename; filenames with this version identifier are used in preference to those without (but a plain .so or .pyd file without the version identifier will be used if no other option is available to import).
向.so或.pyd文件中添加有关针对哪个版本的Python编译的信息有两个主要优点:
There are two main advantages to adding information to the .so or .pyd file about what version of Python it was compiled against:
- 可以将与多个Python版本兼容的.so / .pyd文件全部编译到同一目录中,从而可以更轻松地使用或分发适用于多个Python版本的库。
- 它可以防止意外导入为错误版本的Python编译的模块。这是一件好事,因为ABI在python版本之间不是100%兼容的(但是可能大多数情况下都是兼容的,这会给您带来偶尔和令人困惑的错误)
文件名的更改会影响所有已编译的Python模块,而不仅限于Cython。
The change to the file name affects all compiled Python modules, not just Cython.
此问题描述了如何更改添加的标识符-但是,这样做的理由很少。
This question describes how to change the identifier that's added - there is rarely a good reason for doing so, however.
这篇关于为什么Cython编译器生成带有后缀'cpython-35m-x86_64-linux-gnu.so'的so的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!