如何在Python中获取已安装的GIT的路径?
我需要使用 Python 2.6.1 将 Max OS X 10.6 上的 GIT 路径放入脚本变量中。我使用此代码:
r = subprocess.Popen(shlex.split("which git"), stdout=subprocess.PIPE)
print r.stdout.read()
但问题是输出为空(我也尝试过 stderr)。它可以与其他命令(例如 pwd
或 ls
)配合使用。
有人能帮我吗?
更新:当我从终端运行 which git
时,它会按预期打印出路径。所以,which
可以找到它。
更新 2:我刚刚创建了 bash 脚本
#!/usr/bin/env bash
GP=`/usr/bin/which git`
PWD=`pwd`
echo "PATH IS: ${GP}"
echo "PWD IS: ${PWD}"
,输出是
PATH IS:
PWD IS: /Users/user/tmp
I need to get a path to the GIT on Max OS X 10.6 using Python 2.6.1 into script variables. I use this code for that:
r = subprocess.Popen(shlex.split("which git"), stdout=subprocess.PIPE)
print r.stdout.read()
but the problem is that output is empty (I tried stderr
too). It works fine with another commands such as pwd
or ls
.
Can anyone help me with that?
UPDATE: When I run which git
from Terminal it prints out path as expected. So, which
can find it.
UPDATE 2: I just created the bash script
#!/usr/bin/env bash
GP=`/usr/bin/which git`
PWD=`pwd`
echo "PATH IS: ${GP}"
echo "PWD IS: ${PWD}"
and output is
PATH IS:
PWD IS: /Users/user/tmp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所做的只是迭代 $PATH 中的目录,检查文件是否在那里。只需编写一个小方法即可完成同样的操作。
All which does is iterate over the directories in $PATH, checking to see if the file is there. Just write a small method to do likewise.