0

I am writing a Python 2.7 app that relies on several rpm packages to be installed. There is a planned port to Python 3 in the near future. Is there a simple function call to check if an rpm is installed that works in both versions of Python?

e.g.

rpm = "binutils"
if package_installed(rpm):
    print("{} is installed".format(rpm))
AK47
  • 7,795
  • 6
  • 33
  • 57
mhck
  • 473
  • 1
  • 7
  • 13
  • I imagine you're on a *RH* based *OS*. *YUM* (*RPM* wrapper) is written in *Python*. – CristiFati Dec 17 '19 at 14:03
  • I am on CentOS 7.4. Thanks – mhck Dec 17 '19 at 14:09
  • 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), [How to get list installed Linux rpms with Python?](https://stackoverflow.com/q/34360353/608639) and friends. – jww Dec 17 '19 at 14:46
  • That is sort of a duplicate; more like a similar topic. Some of the answers previously posted is what I was looking for. Thanks – mhck Dec 18 '19 at 11:28

1 Answers1

0
import os

rpm = 'binutils'

f = os.popen('rpm -qa')
arq = f.readlines()
if rpm in arq:
    print("{} is installed".format(rpm))
alexisdevarennes
  • 4,602
  • 3
  • 21
  • 37
  • I had to tweak this to get it to work using Python 2.7 and 3.6. Posted under [How to get list installed Linux rpms with Python?](https://stackoverflow.com/questions/34360353/how-to-get-list-installed-linux-rpms-with-python) – mhck Dec 18 '19 at 12:17