使用Python脚本在Ubuntu中安装Git

时间:2012-06-25 07:11:11

标签: python git ubuntu subprocess

我正在尝试编写一个脚本来检查Ubuntu中的必备软件,如果没有安装,请继续并安装它。

我是Python的新手,我知道Python的subprocess模块可以用来实现这个目标。

我在下面的示例代码中写了这个,但不确定它为什么失败。

import subprocess
c = subprocess.Popen(['sudo','apt-get','install','git'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
grep_stdout = c.communicate(input='root')[0]
print(grep_stdout)

当我运行此程序时,我收到以下错误...

sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts

我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:4)

  

有大量的先决条件。我只是想问   用户安装所有这些将是一个痛苦的脖子。所以就   想到编写一个将测试软件的功能,以及是否   未安装的东西将继续为他安装

有几种方法:

  1. 检查软件是否存在,如果没有退出程序,并显示一条消息,列出要安装的缺失组件。

  2. 提供需要在README或文档中安装的软件列表。

  3. 创建列出所需依赖项的debian package;并让用户使用他们的首选方法安装它。

  4. 如果你的程序必须安装软件本身(它真的必须吗?),那么指示用户使用提升的权限运行你的脚本。在您的文档中,要求他们将其作为root运行。

    顺便说一下sudo可能不会安装在所有系统上。

答案 1 :(得分:1)

import subprocess

c = subprocess.Popen(['sudo','yum','install','git','-y'], 
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
grep_stdout = c.communicate(input='root')[0]

print(grep_stdout)