简体   繁体   中英

In case of RHEL based distribution how do I check which software package manager is used, using python

In case of RHEL family distributions like RHEL, CentOS, Fedora etc, how do I check which software package manager(yum, dnf) is used, using python.

I know, I can check what is the OS, and which version, and based on version decide if dnf or yum is used.

But, it is possible to check without taking in consideration the os version.

You could do the Pythonic way of "ask for forgiveness than permission" by trying to import dnf module first, and if that fails, import yum .

And obviously, if that fails as well, then something is very wrong / no packager manager exists, etc.

manager = 'dnf'    
try:
    import dnf
except ImportError:
    manager = 'yum'  
    try:
        import yum 
    except ImportError:
        manager = None

print(manager)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM