python解释器所在路径咋找_找到Python解释器的完整路径?(Find full path of the Python interpreter?)...

找到Python解释器的完整路径?(Find full path of the Python interpreter?)

在当前执行的Python脚本中,如何找到当前运行的Python解释器的完整路径?

How do I find the full path of the currently running Python interpreter from within the currently executing Python script?

原文:https://stackoverflow.com/questions/2589711

更新时间:2019-11-06 17:12

最满意答案

sys.executable包含当前运行的Python解释器的完整路径。

import sys

print(sys.executable)

这里现在已经记录在案

sys.executable contains full path of the currently running Python interpreter.

import sys

print(sys.executable)

2015-11-09

相关问答

您可能需要将文件名加入包含它的目录中: os.path.realpath(os.path.join(root,name))

例如我刚刚测试过这个: import os

for root, dirs, files in os.walk('.'):

for name in files:

if name == 'foo':

name = str(name)

name = os.path.realpath(os.path.join(ro

...

sys.executable包含当前运行的Python解释器的完整路径。 import sys

print(sys.executable)

这里现在已经记录在案 sys.executable contains full path of the currently running Python interpreter. import sys

print(sys.executable)

which is now documented here

从着名的perl hashbang技巧中获得灵感,我想出了这个: #!/bin/sh

"""":

python2 -c "" 2>/dev/null && exec python2 $0 ${1+"$@"}

python -c "" 2>/dev/null && exec python $0 ${1+"$@"}

echo "Could not find a python interpreter."

exit 1

"""

print "hello python"

Getting inspir

...

因为python 2.7路径是在系统环境变量PATH中设置的。 您正在编辑您的用户变量(这是奇怪的配置,因为它们包含仅在系统路径中找到的重复内容,例如C:\windows\system32 ) 如果你键入where python你可能会得到: C:\Python27\python.exe

(编辑:你真的得到了这个价值,因为你回答了我的评论) 如果你键入where pip你可能会得到: C:\users\jackf_000\sw\anaconda2\scripts\pip.exe

这解释了pip

...

使用win32api.GetLongPathName : >>> win32api.GetLongPathName(r'c:\progra~1')

'c:\\Program Files'

和win32api.GetShortPathName : >>> win32api.GetShortPathName(r'c:\Program Files')

'c:\\PROGRA~1'

注意:您需要安装pywin32才能使用上述功能。 Using win32api.GetLongPathName: >>>

...

使用os.get_exec_path() (可从3.2获得)获取将搜索可执行文件的目录列表。 In [1]: import os

In [2]: paths = os.get_exec_path()

In [5]: for p in paths:

...: for prog in ["python2", "python2.exe"]:

...: attempt = os.path.join(p, prog)

...: if os.pat

...

尝试这个: import os

os.chdir('otherpath')

这至少与您的DOS示例相匹配,并将您的工作目录更改为相对于运行该命令的目录的其他otherpath 。 例如,如果您在/home/myusername/ ,那么这将带您到/home/myusername/otherpath/ 。 你也可以使用. 对于当前目录或..移回一个目录。 因此,如果您在/home/myusername/Desktop/ , os.chdir('..')会将工作目录更改为/home/myuserna

...

实际上,已有一个功能 : for file in pdf_list:

file_name = os.path.basename(file)

upload.file(file_name)

Actually, there is a function for this already: for file in pdf_list:

file_name = os.path.basename(file)

upload.file(file_name)

运行作业时,cron从当前用户主目录调用作业。 在这种情况下,cron运行您的脚本,就好像它位于/home/myname目录中,而不是/home/myname/PycharmProjects/test 。 这意味着Python正在/home/myname查找data1.xml 。 更新代码并将data1.xml更改为绝对路径。 /home/myname/PycharmProjects/test/data1.xml

或者相对于您的主目录 PycharmProjects/test/data1.xml

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值