简体   繁体   中英

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?

In a python script running with Python 3.x, importing another python script running with Python 2.7, is it possible to tell the program to use Python 2.7 interpreter when importing that specific script?

In this "mainfile.py" running with Python 3.x

import anotherfile27

As soon as "anotherfile27.py" is importing, everything will automatically run, no functions in "anotherfile27.py" need to be reused in "mainfile.py". Only several .csv scripts will be generated in order to be used later.

Is it possible to write a line of code within "anotherfile27.py" or within "mainfile.py" to tell the program to change the interpreter from Python 3 to Python 2.7 only when running "anotherfile27.py"?

Here's a basic example:

import subprocess
import platform

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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