240

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

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 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. – mhawke Oct 27 '16 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. – Bhargav Rao Oct 28 '16 at 06: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. – mhawke Oct 28 '16 at 09:02

3 Answers3

400

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
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 3
    Yep - >>> import sys | >>> print sys.executable | C:\Python25\pythonw.exe – Smashery Apr 14 '09 at 23:50
  • 4
    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. – FogleBird Apr 14 '09 at 23:53
  • 1
    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. – John Montgomery Apr 15 '09 at 10:38
  • 6
    "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 :) – GreenAsJade Oct 06 '13 at 03:06
  • Also works on Windows. As for py2exe... there's no way guarantee that python is even installed, in that case. Why would someone bother using py2exe if they figured everyone running their program was going to have python already installed? – ArtOfWarfare Jun 10 '14 at 13:23
  • 1
    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. – Andy Hayden Oct 31 '14 at 00:12
  • It's a convenient way when you won't add the python.exe to the path and wanna call it again in execv or other. – m3nda Mar 11 '16 at 18:24
  • Doesn't the 3.x one work for both? – JeffHeaton Aug 12 '19 at 17:54
63

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

import os
os.__file__
highvelcty
  • 665
  • 5
  • 2
  • 11
    py_interpreter = os.path.join(os.__file__.split("lib/")[0],"bin","python")) – melMass Sep 27 '18 at 15:08
  • 1
    @Akelian You have one too many closing parentheses. Otherwise that's great! – Squanchy Oct 11 '18 at 15:10
  • Didn't work correctly for my system, my lib directory was "Lib" so lowercase "lib/" failed to split. – RufusVS Mar 25 '20 at 20:01
  • just noticed the / is \ on my system (Win10) too Makes an os-agnostic version a little dicier. – RufusVS Mar 25 '20 at 20:08
  • 7
    Maybe rather, `pathlib.Path(os.__file__).parents[2] / 'bin' / 'python'` – rth May 27 '20 at 19:12
  • 4
    Can someone explain why it's not reliable in an embedded python environment? Is that the counter-case the [documentation](https://docs.python.org/3/library/sys.html#sys.executable) means when they say systems that "makes sense"? – MrD Jun 06 '20 at 00:21
  • 1
    @rth `os.__file__` does not work in a venv due to symlinks/hardlinks; it will return the base environment's path for `os.py` so probably need an if statement to check `sys.executable` first, and if it does not end in "`python`", fallback to `os.__file__` – cowbert Feb 04 '21 at 14:19
  • 1
    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](https://docs.python.org/3/library/sys.html#sys.base_prefix). – David Thompson Sep 23 '21 at 16:24
5

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.

Xantium
  • 11,201
  • 10
  • 62
  • 89
Justin
  • 409
  • 3
  • 6
  • If it is in App Paths you don't even need to know the location do you? ;) – Gleb Apr 14 '09 at 23:45
  • 18
    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. – GreenAsJade Oct 06 '13 at 03:04