使用“apt-get install xxx"在 Python 脚本中 [英] Using "apt-get install xxx" inside Python script
问题描述
目前我需要根据操作系统使用 apt 或 rpm 安装一些软件包.我看到了 lib "apt" 来更新或升级系统,但可以用它来安装单个包吗?
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")
但是这个命令显示了shell中的所有进程,我无法隐藏它.
But this command shows all process in the shell, I cannot hide it.
感谢您的帮助.
推荐答案
您可以使用 subprocess
库中的 check_call
.
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)
在这种情况下,将 stdout
转储到 /dev/null
或 os.devnull
.
Dump the stdout
to /dev/null
, or os.devnull
in this case.
os.devnull
是平台无关的,在 POSIX 上返回 /dev/null
,在 Windows 上返回 nul
(这不相关,因为您正在使用 apt-get
但是,还是很高兴知道 :) )
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 :) )
这篇关于使用“apt-get install xxx"在 Python 脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!