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 


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
    Commented Oct 17, 2017 at 18:14

2 Answers 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')


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.


Try the following:

import subprocess

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

  • 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
    Commented Mar 14, 2018 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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