尽管安装了git,但gitpython找不到git命令

问题描述 投票:0回答:1

我已经安装了git python,详细信息:

Python 3.8.1
git version 2.21.1 (Apple Git-122.3)
GitPython==3.1.0
gitdb==4.0.2
OS is Catalina
virtual environment via pyenv, pyenv-virtualenv, pyenv-virtualenvwrapper
git is located in /usr/bin/git

运行此代码:

from git import cmd
import sys

sys.path.append('/usr/bin/')
g = cmd.Git()
g.execute('git') # prints the git help page.
g.execute('git log') # Throws an error. 

g_ = cmd.Git('..')
g_.execute('git'). # prints the git help page.
g_.execute('git log'). # Throws an error. 

错误是:

GitCommandNotFound: Cmd('git') not found due to: FileNotFoundError('[Errno 2] No such file or directory: 'git log'')
cmdline: git log

这实际上是来自第三方库的错误。我已经用更简单的代码复制了该代码中的错误。原始代码调用git remote show origin,它也有类似的错误。

python-3.x git gitpython
1个回答
1
投票

[execute接收命令行参数列表,而不是单个字符串:

g.execute(['git', 'log']) #  Correct!
g.execute('git log') #  Wrong!
g.execute('git') #  Correct because only a single argument
© www.soinside.com 2019 - 2024. All rights reserved.