首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >apt-get安装输出在正常执行时和从python执行时的行为不同。

apt-get安装输出在正常执行时和从python执行时的行为不同。
EN

Stack Overflow用户
提问于 2021-07-26 12:33:14
回答 2查看 150关注 0票数 0

我今天注意到,apt的行为取决于何处(shell / python /.)它是从。

背景我正试图通过apt CLI找出“手表”的提供包。这也很好地手动工作:

代码语言:javascript
复制
apt-get install -s watch

NOTE: This is only a simulation!
  apt-get needs root privileges for real execution.
  Keep also in mind that locking is deactivated,
  so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'procps' instead of 'watch'
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

最重要的是Note, selecting 'procps' instead of 'watch':显然,手表是由procps提供的。到目前一切尚好。

当我从python的subprocess库执行相同的命令时,这一行就消失了。有了os.system,它又开始工作了。

因此,我试图通过简单地将命令行(stdout和stderr)的输出传递到日志文件(apt-get install -s watch &> /tmp/output)结果中来调查这个问题:

代码语言:javascript
复制
NOTE: This is only a simulation!
      apt-get needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists...
Building dependency tree...
Reading state information...
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

线路不见了。

之后,我在apt的源代码中搜索了一下,找到了对应线。这里的输出流是out,其他一些ioprintf部分正在使用c1out。看来这是另一条小溪..。但是为什么我的终端打印这个流的输出,而大多数其他程序都无法处理它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-26 13:07:41

根据输出是否为tty,apt的行为不同。

如果它的stdout不是,它的行为就像--quiet=1被传递给它一样。

您可以通过明确地传递--quiet=0来省略这种行为。

代码语言:javascript
复制
apt-get --quiet=0 install -s watch | less

手册可以告诉我们更多关于--安静。

请注意:这很可能是一个不好的想法,依靠一些apt开发人员变得更难管道,甚至grep/awk。

票数 1
EN

Stack Overflow用户

发布于 2021-07-26 12:40:31

您是从虚拟env运行os.system吗?这是很有可能的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68529911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档