How can I programmatically run another python script with a different python version?
If I am writing a program in python 2.7 and I want to run another script file with a different python (2.6), how can I do this?
EDIT: I am doing this because I need Django (which is installed in python 2.7) and I need some programs that are only available for python 2.6 ...
EDIT2: So I wrote a simple script that will be executed in python 2.6 and I will get the results from it directly in python 2.7
source to share
You have a couple of options, but the most general concept is using os.system
a script to execute.
Explicit interpreter
os.system('python2.6 myscript.py')
Relying on the shebang to choose the right interpreter
os.system('myscript.py')
For this to work, your script must have the first line set to
#!/usr/bin/env python2.6
And your executable python2.6
should be in yours PATH
.
If you need stdin / stdout operation
subprocess.Popen('myscript.py', subprocess.PIPE) #relying on shebang
subprocess.Popen(['/usr/bin/env', 'python2.6', 'myscript.py'], subprocess.PIPE) #manual interpreter selection
See http://docs.python.org/library/subprocess.html#subprocess.Popen
source to share