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?
There are a few approaches to this:
Check if software is there, if it isn’t exit the program with a message listing the missing components to be installed.
Provide a list of software that needs to be installed in
READMEor documentation.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
sudomay not be installed on all systems.