Vijay
Vijay

Reputation: 1815

Installing Git in Ubuntu using Python script

I'm trying to write a script which checks for prerequisite software in Ubuntu and if not installed, goes ahead and installs it.

I'm a newbie to Python and I understand that Python's subprocess module can be used to achieve this goal.

I have written this below sample code but not sure why it is failing.

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)

When I run this program, i get the following error...

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

Am i missing something here?

Upvotes: 0

Views: 743

Answers (2)

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)

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174662

There are significant amount of pre-requisite . I just thought asking user to install all of them would be a pain in the neck. so just thought of writing a function which will test the software and if something not installed will go ahead and install for him

There are a few approaches to this:

  1. Check if software is there, if it isn't exit the program with a message listing the missing components to be installed.

  2. Provide a list of software that needs to be installed in README or documentation.

  3. Create a debian package listing the dependencies required; and have the user install this with their preferred method.

If your program must install the software itself (does it really have to?), then instruct the user to run your script with elevated privileges. In your documentation, ask them to run it as root.

By the way sudo may not be installed on all systems.

Upvotes: 4

Related Questions