繁体   English   中英

在使用Python 3.x的脚本中,使用Python 2.7导入其他脚本时,如何在导入时使该特定脚本与2.7分开运行?

[英]In a script using Python 3.x, importing other script using Python 2.7, how to make that specific script running with 2.7 separately when importing it?

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

在此使用Python 3.x运行的“ mainfile.py”中

import anotherfile27

导入“ 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM