15

currently I need to install some package using apt or rpm, according the OS. I saw the lib "apt" to update or upgrade the system, but it is possible use it to install a single package?

I was trying to use too "subprocess":

subprocess.Popen('apt-get install -y filetoinstall', shell=True, stdin=None, stdout=None, stderr=None, executable="/bin/bash")

But this command shows all process in the shell, I cannot hide it.

Thank you for your help.

Federico Sciarretta
  • 372
  • 2
  • 4
  • 15
  • It seems you are not getting the forked properly. Have you tried reading the documentation? –  Dec 12 '11 at 22:43
  • For Python 3.5+, see [`subprocess.run()`](https://docs.python.org/3/library/subprocess.html#subprocess.run) – phoenix Aug 06 '17 at 18:11

5 Answers5

13

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 :) )

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • 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 – Federico Sciarretta Dec 12 '11 at 23:19
  • 1
    This worked for me. I would like to suggest using a try and except block so that in case of an error it will tell you what the issue is rather than just saying there was an error. I added an answer below as a supplement. – saltchicken Sep 26 '19 at 00:31
5

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 !

Federico Sciarretta
  • 372
  • 2
  • 4
  • 15
3

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

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
  • The link you have shared gives this : Permission Denied You don't have the proper permissions to view this page. Please contact the owner of this project to request permission. – Ayush Nov 06 '17 at 10:36
2

This is meant as an addition to Russell Dias's accepted answer. This adds a try and except block to output actionable error information rather than just stating there was an error.

from subprocess import check_call, CalledProcessError
import os
try:
    check_call(['apt-get', 'install', '-y', 'filetoinstall'], stdout=open(os.devnull,'wb'))
except CalledProcessError as e:
    print(e.output)
saltchicken
  • 170
  • 9
0

Use this to redirect the output to /dev/null:

proc = subprocess.Popen('apt-get install -y filetoinstall', shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
proc.wait()

The call to .wait() will block until the apt-get is complete.

David K. Hess
  • 16,632
  • 2
  • 49
  • 73
  • Ah ok thanks but how can I stop the program, waiting the apt finish the install, and when is installed , procced with the other functions? – Federico Sciarretta Dec 12 '11 at 22:46
  • 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 – Federico Sciarretta Dec 12 '11 at 23:20