简体   繁体   English

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

[英]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.

相关问题 如何从 python 中的多处理模块获取当前正在运行的进程名称 - How to get currently running processes names from multiprocessing module in python 在 Python 中,如何判断模块是否来自 C 扩展? - In Python how can one tell if a module comes from a C extension? 如何从脚本中给出 python 解释器命令? - How can you give the python interpreter commands from within a script? Python原生扩展模块崩溃:解释器能否优雅地失败? - Crash in Python native extension module: Can interpreter fail gracefully? 如何获取当前运行的 python 文件所在的驱动器? - How can I get the drive that the currently running python file is in? 从Rails运行Python解释器 - Running a Python interpreter from Rails 获取 python stdlib 版本,而不是解释器版本 - Get python stdlib version, not interpreter version 如何从具有不同解释器版本的python运行python脚本? - How to run python script from python with different interpreter version? 如何在本地为现有系统python模块安装扩展? - How can one locally install an extension to an existing system python module? 从导入的模块打印当前运行的 python 脚本的 docString - Print the docString of the currently running python script from an imported module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM