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

8 投票
2 回答
3666 浏览
提问于 2025-04-17 05:33

可能重复的问题:
如何在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 个回答

1

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

不管怎样,这里是你想要的内容:

import sys
sys.executable
14

你可以这样做:

>>> import sys
>>> print sys.executable
/usr/bin/python

具体的说明可以参考这里:如何通过编程获取python.exe的位置?

撰写回答