从远程存储库中提取时出现GitPython错误

时间:2016-01-19 14:13:22

标签: python git gitpython

我试图使用以下代码从本地远程仓库中取出:

repo = git.Repo('/home/user/repo/')
o = repo.remotes.origin
try:
    o.pull()
except:
    logging.exception("oops:")

以下追溯记录失败了:

Traceback (most recent call last):
  File "/home/user/my_site/app/app.py", line 58, in regen_logs
    o.pull()
  File "/home/user/my_site/venv/lib/python2.7/site-packages/git/remote.py", line 665, in pull
    proc = self.repo.git.pull(self, refspec, with_extended_output=True, as_process=True, v=True, **kwargs)
  File "/home/user/my_site/venv/lib/python2.7/site-packages/git/cmd.py", line 440, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/home/user/my_site/venv/lib/python2.7/site-packages/git/cmd.py", line 834, in _call_process
    return self.execute(make_call(), **_kwargs)
  File "/home/user/my_site/venv/lib/python2.7/site-packages/git/cmd.py", line 576, in execute
    raise GitCommandNotFound(str(err))
GitCommandNotFound: [Errno 2] No such file or directory

但是,在交互式会话中执行完全正确的三个命令(Repo,设置origin,pull())时,它可以正常工作:

>>> import git
>>> repo = git.Repo('/home/user/repo')
>>> o = repo.remotes.origin
>>> o.pull()
[<git.remote.FetchInfo object at 0x242d2b8>]

我在系统上安装了Git:

$ rpm -q git
git-1.8.3.1-6.el7.x86_64

我在这里做错了什么线索?

2 个答案:

答案 0 :(得分:0)

找到了罪魁祸首。结果我用来启动应用程序的gunicorn,清除大多数环境变量,并定义指向虚拟环境目录的PATH。因此,git可执行文件无法位于任何位置。我通过额外的选项调用gunicorn来解决这个问题:

-e GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git

答案 1 :(得分:0)

你需要在导入git之前设置git的路径,如下所示:

导入os

os.environ ['GIT_PYTHON_GIT_EXECUTABLE'] ='/ usr / local / bin / git'

来自git import Repo