如何在 Python 中调用‘ git pull’?

通过使用 github webhook,我希望能够对远程开发服务器进行任何更改。目前,在适当的目录中,git pull获得需要进行的任何更改。但是,我不知道如何在 Python 中调用该函数。我试过以下方法:

import subprocess
process = subprocess.Popen("git pull", stdout=subprocess.PIPE)
output = process.communicate()[0]

但这会导致以下错误

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

有没有办法在 Python 中调用这个 bash 命令?

129767 次浏览

subprocess.Popen expects a list of the program name and arguments. You're passing it a single string, which is (with the default shell=False) equivalent to:

['git pull']

That means that subprocess tries to find a program named literally git pull, and fails to do so: In Python 3.3, your code raises the exception FileNotFoundError: [Errno 2] No such file or directory: 'git pull'. Instead, pass in a list, like this:

import subprocess
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0]

By the way, in Python 2.7+, you can simplify this code with the check_output convenience function:

import subprocess
output = subprocess.check_output(["git", "pull"])

Also, to use git functionality, it's by no way necessary (albeit simple and portable) to call the git binary. Consider using git-python or Dulwich.

Have you considered using GitPython? It's designed to handle all this nonsense for you.

import git


g = git.cmd.Git(git_dir)
g.pull()

https://github.com/gitpython-developers/GitPython

Try:

subprocess.Popen("git pull", stdout=subprocess.PIPE, shell=True)

This is a sample recipe, I've been using in one of my projects. Agreed that there are multiple ways to do this though. :)

>>> import subprocess, shlex
>>> git_cmd = 'git status'
>>> kwargs = {}
>>> kwargs['stdout'] = subprocess.PIPE
>>> kwargs['stderr'] = subprocess.PIPE
>>> proc = subprocess.Popen(shlex.split(git_cmd), **kwargs)
>>> (stdout_str, stderr_str) = proc.communicate()
>>> return_code = proc.wait()


>>> print return_code
0


>>> print stdout_str
# On branch dev
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   file1
#   file2
nothing added to commit but untracked files present (use "git add" to track)


>>> print stderr_str

The problem with your code was, you were not passing an array for subprocess.Popen() and hence was trying to run a single binary called git pull. Instead it needs to execute the binary git with the first argument being pull and so on.

The accepted answer using GitPython is little better than just using subprocess directly.

The problem with this approach is that if you want to parse the output, you end up looking at the result of a "porcelain" command, which is a bad idea

Using GitPython in this way is like getting a shiny new toolbox, and then using it for the pile of screws that hold it together instead of the tools inside. Here's how the API was designed to be used:

import git
repo = git.Repo('Path/to/repo')
repo.remotes.origin.pull()

If you want to check if something changed, you can use

current = repo.head.commit
repo.remotes.origin.pull()
if current != repo.head.commit:
print("It changed")

If you're using Python 3.5+ prefer subprocess.run to subprocess.Popen for scenarios it can handle. For example:

import subprocess
subprocess.run(["git", "pull"], check=True, stdout=subprocess.PIPE).stdout