如何从 Python 中调用“git pull"? [英] How can I call 'git pull' from within Python?

查看:29
本文介绍了如何从 Python 中调用“git pull"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 github webhooks,我希望能够将任何更改拉到远程开发服务器.目前,当在适当的目录中时,git pull 获取需要进行的任何更改.但是,我不知道如何从 Python 中调用该函数.我尝试了以下方法:

Using the github webhooks, I would like to be able to pull any changes to a remote development server. At the moment, when in the appropriate directory, git pull gets any changes that need to be made. However, I can't figure out how to call that function from within Python. I have tried the following:

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 命令?

Is there a way that I can call this bash command from within Python?

推荐答案

您是否考虑过使用 GitPython?它旨在为您处理所有这些废话.

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

这篇关于如何从 Python 中调用“git pull"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆