BirdZhang
BirdZhang

Reputation: 21

How to get list installed Linux rpms with Python?

I use subprocess.getoutput("rpm -qa").split("\n"),it's not very well. rpmfile module can only read .rpm files

Can you help me find one module?

Upvotes: 1

Views: 5525

Answers (3)

mhck
mhck

Reputation: 1051

I modified code similar to what is posted by Marcus Poli. This was tested using Python 2.7 and 3.6 on CentOS 7.4. My original question was How do I check if an rpm package is installed using Python?

import os
rpm = 'binutils'
f = os.popen('rpm -qa')
arq = f.readlines()
for r in arq:
   if rpm in r:
      print("{} is installed".format(r.rstrip()))

Output:

binutils-devel-2.27-34.base.el7.x86_64 is installed
binutils-2.27-34.base.el7.x86_64 is installed

Upvotes: 1

Marcus Poli
Marcus Poli

Reputation: 51

Maybe code below is useful for someone.

import os
f = os.popen('rpm -qa')
arq = f.readlines()
#print("First file=" + arq[0].strip())
for x in arq:
    print(x) 

Upvotes: 1

skytux
skytux

Reputation: 1306

If you are using Fedora, there is a module called rpm from the package rpm-python that will allow you to query the rpm database:

import rpm

ts = rpm.TransactionSet()
mi = ts.dbMatch()
for h in mi:
    print "%s-%s-%s" % (h['name'], h['version'], h['release'])

That is a simple piece of code from the documentation. See here for more information.

Upvotes: 11

Related Questions