当python代码转换为.pyc文件时,是否有方法检查python解释器版本?

2024-05-23 15:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用py_compile将python文件(.py)转换为.pyc文件

py_compile.compile(py_src_file, doraise=True)

python脚本包含同时具有python版本2.7和3.8语法的代码。 与python 3.8版本一起使用时py_compile会为相应的2.7语法生成错误。有没有办法在编译代码时检查python版本,并只编译相应的指令

例如:

if python_version == 2.7:
   # py_compile should see 2.7 code
else:
   # py_compile should see 3.8 code

在这个答案中我得到了一些帮助:How would you do the equivalent of preprocessor directives in Python?,但不知道如何在我的案例中准确使用。 谢谢你的帮助


Tags: 文件代码py版本src脚本true语法