1. Launch Python 3.x from under python 2.x (or IronPython)

  2. Run a script written in Python 3.x

  3. If possible, get the output and return it to Python 2.x

PS And is it possible to place the body of the script for Py3 in the body of Py2?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

If python3 installed on the computer, for example, you can run a script interpreter through the subprocess module.

An example of running the python interpreter. sys.executable - the full path to the interpreter, you can replace, for example, the line "C: \ Python3 \ python.exe", which executes the script <script>.py . The script <script>.py writes something to the console, we catch it and write to our console:

 from subprocess import Popen, PIPE import sys if __name__ == '__main__': with Popen([sys.executable, '-u', '<script>.py'], stdout=PIPE, universal_newlines=True) as process: for line in process.stdout: print(line, end='') 
  • Can I find out more about your option? - Irish_Tactical_Beard
  • one
    Updated the answer code - gil9red
  • Yes thank you! Exactly what is needed! And is there a possibility in the body of the main code to hide the lines to be sent to another python? Those. do not create additional '<script> .py' - Irish_Tactical_Beard
  • Those. Do you want to pass arguments ( sys.argv ) to a running script? - gil9red
  • In my case, let's say the script body is for Py2. * PY = r'C: \ python3 \ python.exe 'scr = r'C: \ Users \ Man \ Desctop \ test.py' (script for Py3. *) If name == ' main ': with Popen ([PY, '-u', SCR], stdout = PIPE, universal_newlines = True) as process: for line in process.stdout: print (line, end = '') And here’s how once everything that is contained in the "SCR" I wanted to be here and leave without causing an additional launch of the file - Irish_Tactical_Beard