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

2
  • May i refer to you to the following question with the same question: stackoverflow.com/questions/2547554/… Your question is asked there. :) Commented Mar 27, 2012 at 16:50
  • There are several ways to call external process in python. See this question for details Commented Mar 27, 2012 at 16:51

1 Answer 1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.