1

I have a system with many versions of python. Some are 32b some 64b, some 2.4 some 2.7, etc. I am working with a python script that needs to run another python script. I want to run the 2nd script with the same version of python that the initial script was run with.

My plan is to do something like this:

os.system('%s script.py' % currentPython)

The question is: How do I define "currentPython"?

0

3 Answers 3

5

The path to the python interpreter is accessible as sys.executable.

Please use the subprocess module to call a sub-interpreter, and not os.system().

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

Comments

3

sys.executable contains the path you're looking for. However, you should not use os.system, as that path may contain spaces (for example in C:\Program Files\Python2.7\python.exe). Instead, use subprocess.check_call:

subprocess.check_call([sys.executable, 'script.py'])

Comments

1

Use sys.executable. It gives

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense.

>>> import sys
>>> sys.executable
'C:\\Panda3D-1.7.2\\python\\python.exe'
>>>

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.