如何在Python中获取已安装的GIT的路径?

发布于 2024-08-25 03:17:05 字数 594 浏览 1 评论 0原文

我需要使用 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)。它可以与其他命令(例如 pwdls)配合使用。

有人能帮我吗?

更新:当我从终端运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-09-01 03:17:05

所做的只是迭代 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文