【发布时间】:2013-02-25 06:30:30
【问题描述】:
使用 github webhooks,我希望能够将任何更改拉到远程开发服务器。目前,当在适当的目录中时,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 命令?
【问题讨论】:
-
@Brandon 这不是真的,还有很多其他的解决方案,最好的。
-
git是否在 PATH 中可执行? -
@jleahy 也许,据我了解,celecnius 正在有效地询问“我如何运行 bash 命令”。这个问题已经被问过很多次了。
-
这是一个老问题,但我认为
subprocess.Popen现在有一个cwd关键字参数,它将在指定目录中执行命令。例如:subprocess.Popen(['git', 'pull', '-v', 'origin', 'master'], cwd='/path/to/git/repo', ...)