How can I call 'git pull' from within Python?
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
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.
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")