【问题标题】:Python show all installed packagesPython 显示所有已安装的包
【发布时间】:2014-09-29 17:21:41
【问题描述】:

python 有没有办法显示安装在服务器上的所有 apt/yum 包?我有一个程序只能获取我指定的一个包,但我想知道 python 中是否有类似 apt-show-versions/yum check-update 的模块,因为 python-yum 和 python-apt 只做单个包.

谢谢。

编辑:

这是我目前拥有的代码:

# For finding the package version and using the package name -i
def aptpkg(package_name):
    cache = apt.Cache()
    pkg = cache[package_name]
    host = subprocess.Popen('hostname', stdout=subprocess.PIPE, universal_newlines=True).stdout.read().strip()
    if pkg.is_installed:
        print host
        print 'Current ' + package_name + ' installed:', pkg.installed.version
        con.execute("insert into ansible_packagelist(date, host, package_name, installed_version) values (current_timestamp,%s,%s,%s)", (host, package_name, pkg.installed.version,))
    else:
        print host, package_name + ' is not installed on this system.\n'
    if pkg.is_upgradable:
        print 'Upgradeable version of ' + package_name + ' :', pkg.candidate.version
        con.execute("update ansible_packagelist set upgradeable_version = %s where package_name = %s", (pkg.candidate.version, package_name))
    db.commit()

【问题讨论】:

标签: python linux yum apt


【解决方案1】:

这是pythonic的方式:

import apt
cache = apt.Cache()

for mypkg in cache:
    if cache[mypkg.name].is_installed:
        print mypkg.name

对于 yum,我需要先登录到 RH/Centos 系统。 但是你可以看看 yum 库,你会发现类似“rpmdb.searchNevra”的东西

【讨论】:

  • 我觉得第二行的cache = apt.Cache()是多余的?
【解决方案2】:

使用subprocess 运行shell 命令。

from subprocess import call

适合:call(["dpkg", "-l"])

百胜:call(["yum list installed"])

【讨论】:

  • 感谢您的帮助,但我正在寻找一个模块而不是 bash 解决方法。
  • @Nvasion 也许你想使用import socket; host = socket.gethostname(); 来避免主机名子进程。
【解决方案3】:

maxadamo 的回答是正确的,但是Cache 被初始化了两次不知道为什么。

您可以只实例化一个缓存,循环遍历它并直接在 for 循环中访问 pkg。您不需要额外的查找,因为您已经可以访问pkg 的属性。

所以这里有一个更 Pythonic 的方式:

import apt
cache = apt.Cache()

for pkg in cache:
    if pkg.is_installed:
        print pkg.name

【讨论】:

  • 你可以修复我的回复 :)
【解决方案4】:
import re

with open("/var/lib/dpkg/status", "r") as f:
    status = f.read()
    packages_installed = re.findall("Package: (.*)\nStatus: install ok installed", status)

您必须阅读有关 dpkg 软件包状态的信息。执行:“man dpkg”并转到“关于包的信息”部分。

我认为对于某些问题使用模块是不必要的。它会创建依赖项(我不喜欢那样)。并且“简单”脚本比使用 apt 模块的脚本更快。 比较:

a.py:

    import re

with open("/var/lib/dpkg/status", "r") as f:
        status = f.read()
        packages_installed = re.findall("Package: (.*)\nStatus: install ok installed", status)

b.py:

 import apt
cache = apt.Cache()

for pkg in cache:
    pass
#        if pkg.is_installed:
#            print pkg.name

次:

/tmp » time python a.py
    python a.py  0,04s user 0,01s system 58% cpu 0,075 total
/tmp » time python b.py
    python b.py  3,07s user 0,09s system 75% cpu 4,205 total

如果唯一目标是获取已安装软件包的列表,则无需使用 apt 模块。其他方法是使用子进程并弹出“dpkg -l”,但我不是 exec 的朋友。

【讨论】:

    【解决方案5】:

    如果有人想使用“import re”回答

    收到通知: 有些包有另一个标题

    Package: perl-base
    Essential: yes
    Status: install ok installed
    

    所以这段代码不起作用

    如果你仍然想使用它,需要进行如下更改:

    re.findall("Package: (.*)\n(Essential: yes\n){,1}Status: install ok installed", status)
    

    【讨论】:

      【解决方案6】:

      您可以使用pip listpip list > requirements.txt 手动处理文件以生成需求。

      【讨论】:

        猜你喜欢
        • 2021-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-02
        • 2019-10-26
        • 2018-10-31
        • 1970-01-01
        相关资源
        最近更新 更多