2

Can I use a script written in python 3.3 with the subprocess function to create a pipe between my script and another written in 2.7 ?

If the answer is yes should I be doing something like this?

import subprocess

output = subprocess.check_output(['scriptInV2.7.py', '-arg1', '-arg2'])
myFunctionInV3.3(output)

I am pretty new to python, sorry for my lack of comprehension

4
  • So you just want the output of one passed to another? Also have you actually tried what you have suggested? Commented May 6, 2015 at 9:35
  • @PadraicCunningham is it possible to run two versions Python2.x and Python3.x parallel on same machine ? Does that Shabang #! can do something here ? Commented May 6, 2015 at 9:42
  • @PadraicCunningham I want to pass argument from one script in Python3.3 to another script in Python2.7 and recover the result in the first script (Python3.3). I did not tried because I want to be sure that will be working before using this solution Commented May 6, 2015 at 9:47
  • Just try having the explicit path to the other Python you want to call before your script location, subprocess.check_output(['C:/Path/To/PythonX.X/Python.exe', 'scriptInV2.7.py', '-arg1', '-arg2']) Commented May 6, 2015 at 9:49

2 Answers 2

4

You can use your suggested approach. Either specify the python executable in the shebang (you need both python 2 and 3 installed in parallel):

#! /usr/bin/env python2

and (in your python 3 calling script):

#! /usr/bin/env python3

or you can specify the interpreter when you are calling the script:

output = subprocess.check_output(['/usr/bin/python2', 'scriptInV2.7.py', '-arg1', '-arg2'])

(update with the path to your python2 executable).

Sign up to request clarification or add additional context in comments.

1 Comment

note: the shebang approach is more flexibly (it works even on Windows via Python launcher).
0

Yes you can do so. However check_output normally takes an executable file as the first parameter, so scriptInV2.7.py needs to have execute permission set and have a shebang line that invokes python2.7.

You probably better explicitly specify the python executable as the first list element.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.