我能否从Python内部找到Python可执行文件的路径?

7
可能重复:如何从Python脚本内部获取当前Python解释器路径? 标题已经非常明确了。我想知道在Python内部正在使用哪个Python可执行文件。类似于
Python 2.7.2 (default, Nov 1 2011, 03:31:17)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print <insert the code I'm after here>
/usr/local/bin/python2.7
>>>
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print <insert the code I'm after here>
/usr/bin/python2.6
>>>
你可以自己试试。谢谢
2个回答

13

1

为什么不直接使用bash的where命令呢?

无论如何,这就是你要找的:

import sys
sys.executable

也许他需要特定实例的可执行文件名,它可以是任何与Python库链接的程序。 - lvella
太好了!- 等等,为什么我没有被点赞或者票选?o.O - A T
1
它是 which python 命令,不是 where 命令!!! - shahjapan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接