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