Search code examples
pythongitgitpython

GitPython error when pulling from remote repository


I'm trying to pull from a remote repo in a local one with this code:

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

This fails miserably with the following traceback:

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

However, when doing the exact three commands (Repo, set origin, pull()) in an interactive session, it works just fine:

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

I do have Git installed on the system:

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

Any clues what I'm doing wrong here?


Solution

  • Found the culprit. Turns out gunicorn, which I used to start the app, clears out most environment variables, and defines a PATH pointing to the virtual environment's directory. Thus, the git executable could not be located anywhere. I fixed this by calling gunicorn with an extra option:

    -e GIT_PYTHON_GIT_EXECUTABLE=/usr/bin/git