Check if a Debian package is installed from Python

后端 未结 7 1542
一生所求
一生所求 2020-12-31 03:20

Is there an elegant and more Python-like way to check if a package is installed on Debian?

In a bash script, I\'d do:

dpkg -s packagename | grep Stat         


        
7条回答
  •  半阙折子戏
    2020-12-31 03:58

    I needed a cross-platform compatible solution so I ended up using which.

    import subprocess
    retval = subprocess.call(["which", "packagename"])
    if retval != 0:
        print("Packagename not installed!")
    

    Although it's not as pythonic as the above answers it does work on most platforms.

提交回复
热议问题