How to get the python.exe location programmatically?
Question:
Basically I want to get a handle of the python interpreter so I can pass a script file to execute (from an external application).
Answers:
I think it depends on how you installed python. Note that you can have multiple installs of python, I do on my machine. However, if you install via an msi of a version of python 2.2 or above, I believe it creates a registry key like so:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp PathsPython.exe
which gives this value on my machine:
C:Python25Python.exe
You just read the registry key to get the location.
However, you can install python via an xcopy like model that you can have in an arbitrary place, and you just have to know where it is installed.
This works in Linux & Windows:
Python 3.x
>>> import sys
>>> print(sys.executable)
C:pathtopython.exe
Python 2.x
>>> import sys
>>> print sys.executable
/usr/bin/python
sys.executable is not reliable if working in an embedded python environment. My suggestions is to deduce it from
import os
os.__file__
Basically I want to get a handle of the python interpreter so I can pass a script file to execute (from an external application).
I think it depends on how you installed python. Note that you can have multiple installs of python, I do on my machine. However, if you install via an msi of a version of python 2.2 or above, I believe it creates a registry key like so:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp PathsPython.exe
which gives this value on my machine:
C:Python25Python.exe
You just read the registry key to get the location.
However, you can install python via an xcopy like model that you can have in an arbitrary place, and you just have to know where it is installed.
This works in Linux & Windows:
Python 3.x
>>> import sys
>>> print(sys.executable)
C:pathtopython.exe
Python 2.x
>>> import sys
>>> print sys.executable
/usr/bin/python
sys.executable is not reliable if working in an embedded python environment. My suggestions is to deduce it from
import os
os.__file__