How to run my script with one version of python from script with another version of python? Also, is there a way to catch all outputs of my script and print it?
Asked
Active
Viewed 2,014 times
1
-
1You should only ask one question per, ehm, question. – dwitvliet Jul 21 '14 at 19:18
-
Not sure I understand your second question. The output of your script is already being printed. If it wasn't, it wouldn't be called output. – Kevin Jul 21 '14 at 19:18
-
Take a look at the `subprocess` module. – Lev Levitsky Jul 21 '14 at 19:18
-
Answer to the first question: Depends on the environment(Windows,Linux, etc). Answer to the second: print statement – Dudemanword Jul 21 '14 at 19:19
1 Answers
1
So you want to theoretically run say a script with python2 from a script running with python3? (Or something like that, I'm not really sure) If so I would use subprocess. You can find more information on that https://docs.python.org/2/library/subprocess.html and How do I execute a program from python? os.system fails due to spaces in path.
#my python3 script
import subprocess
subprocess.call(["python2", "python2scipt.py"])

Community
- 1
- 1

Donkyhotay
- 73
- 6
-
1Better use `subprocess.call(["python2", "python2script.py"])` - each part of command line call being one item in the list. – Jan Vlcinsky Jul 21 '14 at 19:27
-
Thanks a lot. It worked for me: `output = subprocess.Popen(p, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE).communicate()[0]` – alby Jul 21 '14 at 19:31
-
Oops, you're right I did forget those are necessary when using subprocesses. Thanks for the catch. I've corrected my example. – Donkyhotay Jul 21 '14 at 19:32