Python 显示所有已安装的包

标签 python linux yum apt

有没有办法让 python 显示服务器上安装的所有 apt/yum 包?我有一个程序只能抓取我指定的一个包,但我想知道是否有 apt-show-versions/yum check-update 像 python 中的模块,因为 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()

最佳答案

这是 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”的内容

关于Python 显示所有已安装的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105433/

相关文章:

linux - 替换 sed 中的控制字符

linux - 使用 yum 在 CentOS 上更新 MongoDB 2.6 错误

python - 使用 Graphlab Create Jupyter 时出错

Linux命令根据字符分割文件中的每一行并仅将指定列写入另一个文件

python - 无法导入 flask ,因为 werkzeug

c - ncurses 和 getch - 处理删除和箭头字符

centos - centos 5.6上安装httpd-devel apr-devel apr-util-devel yum时如何解决依赖包

cmake - yum 删除 cmake 失败

python - for循环代码块

python - 如何查看 RabbitMQ 中的排队任务?