Using "apt-get install xxx" inside Python script

For this particular task, as an alternative to subprocess you might consider using Fabric, a python deployment tool to automate builds.


Thank guys ! I use part of each solution. My code:

proc = subprocess.Popen('apt-get install -y FILE', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
proc.wait()

Added: stdout and .wait

Thank you one more time from Argentina !


You can use check_call from the subprocess library.

from subprocess import STDOUT, check_call
import os
check_call(['apt-get', 'install', '-y', 'filetoinstall'],
     stdout=open(os.devnull,'wb'), stderr=STDOUT) 

Dump the stdout to /dev/null, or os.devnull in this case.

os.devnull is platform independent, and will return /dev/null on POSIX and nul on Windows (which is not relevant since you're using apt-get but, still good to know :) )