Using “apt-get install xxx” inside Python script

前端 未结 5 749
既然无缘
既然无缘 2020-12-15 10:52

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 si

5条回答
  •  难免孤独
    2020-12-15 11:31

    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)
    

提交回复
热议问题