Pythonic way to check if a package is installed or not

前端 未结 4 1805
小鲜肉
小鲜肉 2020-12-20 00:46

Pythonic way to check list of packages installed in Centos/Redhat?

In a bash script, I\'d do:

 rpm -qa | grep -w packagename
4条回答
  •  没有蜡笔的小新
    2020-12-20 01:19

    I could not get this answer: https://stackoverflow.com/a/51258124/498657 to work on Python 3.6.8, what did work for me was:

    import sys
    import rpm
    
    ts = rpm.TransactionSet()
    mi = ts.dbMatch( 'name', 'lsof' )
    
    rpmhit=0
    for h in mi:
        if h['name'] == 'lsof':
            rpmhit=1
            break
    
    if rpmhit == 0:
        print('Error: Package lsof not installed. Install using: dnf install lsof')
        sys.exit(3)
    

提交回复
热议问题