4

If I write program in python 2.7 and I want to run another script file with another python (2.6) how can I do this?

EDIT: I do 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 simple script, that will be executed in python 2.6 and I will get results from it right in python 2.7

Cœur
  • 37,241
  • 25
  • 195
  • 267
SuitUp
  • 3,112
  • 5
  • 28
  • 41
  • There are several ways to call external process in python. See [this](http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python) question for details – Roman Bataev Mar 27 '12 at 16:51
  • May i refer to you to the following question with the same question: http://stackoverflow.com/questions/2547554/official-multiple-python-versions-on-the-same-machine Your question is asked there. :) – IT Ninja Mar 27 '12 at 16:50

1 Answers1

5

You have several options, but the most general concept is to use os.system to do your script execution.

Explicit interpreter

os.system('python2.6 myscript.py')

Relying on shebang for choosing the right interpreter

os.system('myscript.py')

For this to work your script has to have the first line set to

#!/usr/bin/env python2.6

And your python2.6 executable needs to be in your PATH.

If you need stdin/stdout manipulation

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

Irfy
  • 9,323
  • 1
  • 45
  • 67