1

Is it possible to install software using python.py scripts? I wanted the python script to do a sudo command on the Raspberry Pi. I want the python script to be able to update or install a software in the Raspberry Pi just by running the script.

def index():

    return sudo apt-get update 

index()

where in this case the Raspberry Pi will update when I run the script above.

  • thank you for the answer both of you, they are all working – user74911 Oct 17 '17 at 18:14
2

Use the os module for this purpose. Your sudo command is meant to be entered into a terminal and that is what the os.system() function does (it takes your shell command as a string):

import os

def install_function():
    os.system('sudo apt-get update')

install_function()

You probably will still need to enter your passwort.

If you want to return the answer of your shell command, use returnstring=os.popen('<your shell command>'). But be aware that os.popen() does not work with interactive shell.

2

Try the following:

import subprocess

def index():
    subprocess.call("sudo apt-get update", shell=True)

index()
  • In what ways would this be different or potentially better? I'm trying to learn how to issue commands from within a Python script (see here and here) and having some problems. I wonder if subprocess might be better in my case as well. – uhoh Mar 14 '18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.