简体   繁体   中英

using both Python3 and Python2.7 in one app

I have problem such is I have to use two scripts, one which is compatible only with Python2.7 and second which is compatible only with Python3.

So my question is if it is possible in any way to do that? (not refactoring the code) I thought about using execfile() but it also use only one compiler.

From what you say, these scripts are independent - you can't share variables between 2.7 and 3. How do you communicate between them?

You could:

  • Convert the 2.7 script to 3, using the 2to3 script provided by Python3. So you don't have to refactor manually. This usually works quite well.

  • Run one script, and call the other script from there calling, for example, subprocess to execute the other using the correct interpreter. Something like (from the 2.7 script):

    subprocess.call(['python3', 'other_script.py']) // or result = subprocess.check_output(['python3', 'other_script.py']) // if you need the script's output

  • Write a small bash (or .bat) and call one and the other.

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