相关文章推荐
含蓄的绿茶  ·  python ...·  1 周前    · 
忧郁的皮带  ·  python之subprocess.Pope ...·  1 周前    · 
逆袭的大海  ·  vs ...·  4 天前    · 
高大的凉茶  ·  Springboot 使用Jackson ...·  11 月前    · 
成熟的橡皮擦  ·  vba selenium send ...·  1 年前    · 
焦虑的双杠  ·  Shader Storage ...·  1 年前    · 
乖乖的树叶  ·  使用Spring Data ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams @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, 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. Bhargav Rao Oct 28, 2016 at 6:46 @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, 2016 at 9:02 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, 2009 at 23:53 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, 2009 at 10:38 "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 6, 2013 at 3:06 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, 2014 at 0:12

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

import os
os.__file__
                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"?
– MrD
                Jun 6, 2020 at 0:21
                @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 4, 2021 at 14:19

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.

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 6, 2013 at 3:04