gpt4 book ai didi

python - 如何从 Python 扩展模块中获取当前运行的 Python 解释器的版本?

转载 作者:行者123 更新时间:2023-11-28 22:52:43 24 4
gpt4 key购买 nike

这个问题是关于如何从扩展模块(即用 C 编写的)中检测用于执行扩展模块的 Python 解释器的版本。

作为背景,在 Python 扩展模块中获取编译扩展所针对的 Python 版本非常简单。您可以只使用 macros defined in patchlevel.h 之一在包含标准 Python 头文件 Python.h 时包含它们(例如宏 PY_VERSION)。

我的问题是是否有可能在运行时从扩展模块中获取当前用于运行扩展 的解释器版本。例如,如果 Python 2.5 被意外地用于运行针对 Python 2.7 编译的扩展模块,我希望能够在运行时从扩展模块中检测到这一点。具体而言,我们假设扩展模块是针对 Python 2.7 编译的。

最佳答案

使用 Py_GetVersion() API :

Return the version of this Python interpreter. 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; 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.

关于python - 如何从 Python 扩展模块中获取当前运行的 Python 解释器的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223704/

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