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.

Quintin Balsdon
  • 681
  • 5
  • 19
user74911
  • 13
  • 1
  • 3

2 Answers2

2

Try the following:

import subprocess

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

index()
Quintin Balsdon
  • 681
  • 5
  • 19
  • 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 08:42
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.

Sim Son
  • 671
  • 3
  • 12