[英]How can one get the version of the currently-running Python interpreter from within a Python extension module?
This question is about how to detect the version of the Python interpreter being used to execute an extension module, from within an extension module (ie one written in C). 这个问题是关于如何从扩展模块(即用C编写的)中检测用于执行扩展模块的Python解释器的版本。
As background, it's straightforward inside a Python extension module to get the version of Python against which the extension was compiled. 作为背景,在Python扩展模块内部很简单,就可以获取针对其编译扩展的Python版本。 You can just use one of the macros defined in
patchlevel.h
that are included when including the standard Python header Python.h
(eg the macro PY_VERSION
). 您可以仅使用
patchlevel.h
中定义的宏之一,其中包含标准Python标头Python.h
(例如,宏PY_VERSION
)。
My question is whether it's possible from within an extension module to get at run-time the version of the interpreter currently being used to run the extension . 我的问题是,是否可以从扩展模块内部在运行时获取当前用于运行扩展的解释器的版本。 For example, if Python 2.5 is accidentally being used to run an extension module compiled against Python 2.7, I'd like to be able to detect that at run-time from within the extension module.
例如,如果偶然使用Python 2.5运行针对Python 2.7编译的扩展模块,我希望能够在运行时从扩展模块中检测到它。 For concreteness, let's assume the extension module is compiled against Python 2.7.
为了具体起见,我们假设扩展模块是针对Python 2.7编译的。
Use the Py_GetVersion()
API : 使用
Py_GetVersion()
API :
Return the version of this Python interpreter.
返回此Python解释器的版本。 This is a string that looks something like
这是一个看起来像的字符串
"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
The first word (up to the first space character) is the current Python version;
第一个单词(直到第一个空格字符)是当前的Python版本; the first three characters are the major and minor version separated by a period.
前三个字符是主要和次要版本,以句点分隔。 The returned string points into static storage;
返回的字符串指向静态存储; the caller should not modify its value.
调用方不应修改其值。 The value is available to Python code as
sys.version
.该值可作为
sys.version
用于Python代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.