Using “apt-get install xxx” inside Python script

前端 未结 5 746
既然无缘
既然无缘 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:38

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

提交回复
热议问题