1

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?

jww
  • 97,681
  • 90
  • 411
  • 885
BirdZhang
  • 21
  • 1
  • 6
  • Possible duplicate of [Pythonic way to check if a package is installed or not](https://stackoverflow.com/q/27833644/608639), [Determine if package installed with Yum Python API?](https://stackoverflow.com/q/8439074/608639), [Check if one package is installed in my system with Python?](https://stackoverflow.com/q/24940797/608639) and friends. – jww Dec 17 '19 at 14:51

3 Answers3

10

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.

skytux
  • 1,236
  • 2
  • 16
  • 19
1

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) 
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
1

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
mhck
  • 873
  • 1
  • 10
  • 20