尽管已安装 git,但 gitpython 无法找到 git 命令

gitpython can't find git commands despite git being installed

我已经安装了 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 也有类似的错误。

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

g.execute(['git', 'log']) #  Correct!
g.execute('git log') #  Wrong!
g.execute('git') #  Correct because only a single argument