在使用 Python 3.x 的脚本中,使用 Python 2.7 导入其他脚本,如何在导入时使该特定脚本单独使用 2.



在运行Python 3.x的python脚本中,导入另一个运行Python 2.7的python脚本,是否可以告诉程序在导入该特定脚本时使用Python 2.7解释器?

在这个运行 Python 3.x 的"mainfile.py"

导入另一个文件27

一旦导入"anotherfile27.py",一切都会自动运行,"anotherfile27.py"中的任何功能都需要在"mainfile.py"中重用。只会生成几个.csv脚本以供以后使用。

是否可以在"anotherfile27.py"或"mainfile.py"中编写一行代码,告诉程序仅在运行"anotherfile27.py"时将解释器从Python 3更改为Python 2.7?

下面是一个基本示例:

import subprocess
import platform
print("Running ", platform.python_version())
old_version = subprocess.check_output("python2 --version", encoding="utf-8")
print("Old version", old_version)

最新更新