使用"apt-get install xxx"内Python脚本 [英] Using "apt-get install xxx" inside Python script

查看:120
本文介绍了使用"apt-get install xxx"内Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,根据操作系统,我需要使用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.

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

stdout转储到/dev/nullos.devnull.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆