微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

apt-get install 输出在正常执行和从 python 执行时的行为不同

如何解决apt-get install 输出在正常执行和从 python 执行时的行为不同

我今天注意到 apt 的行为取决于它运行的位置(shell / python / ...)。

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

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':显然,watch 是由 procps 提供的。到目前为止一切顺利。

当我从 python 的 subprocess 库中执行相同的命令时,这一行消失了。使用 os.system,它又可以工作了。

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

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 to remove and 15 not upgraded.

缺少该行。

之后我在apt的源代码搜索了一下,找到了corresponding line。这里的输出流是 out,其他一些 ioprintf 部分正在使用 c1out。所以这似乎是另一个流......但是为什么我的终端打印了这个流的输出而大多数其他程序无法处理它?

解决方法

apt 的行为取决于其输出是否为 tty。

如果它的标准输出不是,它的行为就像 --quiet=1 已传递给它一样。

您可以通过显式传递 --quiet=0 来省略该行为。

apt-get --quiet=0 install -s watch | less

手册页可以提供更多关于 --quiet 的信息。

请注意: 依赖 apt 开发人员使管道甚至 grep/awk 变得更难的东西很可能是个坏主意。

,

您是否在虚拟环境中运行 os.system? 很有可能是因为这个。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。