267

Basically I want to get a handle of the python interpreter so I can pass a script file to execute (from an external application).

3
  • 4
    @Bhargav Rao: how can this question, which was asked 1 year before the question that it is supposed to be a duplicate of, be a duplicate of it? It's the other way around. Commented Oct 27, 2016 at 22:58
  • @mhawke The other had more views than this and was better worded. Hence I duped it in the reverse direction. TBH, Both of them does say the same thing, So we can even flag for merger. Commented Oct 28, 2016 at 6:46
  • 3
    @BhargavRao: yes, it is better written and the title is probably responsible for that. Also the accepted answer is better and (now) includes a link to the documentation, so overall I think you're right. Commented Oct 28, 2016 at 9:02

3 Answers 3

438

This works in Linux & Windows:

Python 3.x

>>> import sys
>>> print(sys.executable)
C:\path\to\python.exe

Python 2.x

>>> import sys
>>> print sys.executable
/usr/bin/python
Sign up to request clarification or add additional context in comments.

9 Comments

Yep - >>> import sys | >>> print sys.executable | C:\Python25\pythonw.exe
I think in a py2exe compiled app, it would point to the executable for the app, and not the python.exe. Someone would have to confirm though.
That only makes sense if you are already running the Python interpreter. I think he's trying to find the location from outside of Python itself.
"programmatically" I think means from within the python interpretter. At least, that's what it meant for me when I can searching for this answer :)
As mentioned below this does't work e.g. in an environment or if you have multiple python versions installed. Obviously here you want the python version currently in use.
|
69

sys.executable is not reliable if working in an embedded python environment. My suggestions is to deduce it from

import os
os.__file__

8 Comments

py_interpreter = os.path.join(os.__file__.split("lib/")[0],"bin","python"))
@Akelian You have one too many closing parentheses. Otherwise that's great!
Maybe rather, pathlib.Path(os.__file__).parents[2] / 'bin' / 'python'
Can someone explain why it's not reliable in an embedded python environment? Is that the counter-case the documentation means when they say systems that "makes sense"?
I was able to leverage sys.base_prefix and sys.prefix for the base python installation and virtual environment, respectively. This was convenient since I was already importing sys. More description in the docs.
|
2

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_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe

which gives this value on my machine:

C:\Python25\Python.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.

2 Comments

If it is in App Paths you don't even need to know the location do you? ;)
Looking in the registry is not really much use, because you don't know if you are running the python that the one in the registry is talking about.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.