Check if a Debian package is installed from Python

后端 未结 7 1548
一生所求
一生所求 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:47

    If you are checking for the existence of a package that installs a Python module, you can test for this from within a dependent Python script - try to import it and see if you get an exception:

    import sys
    try:
        import maybe
    except ImportError:
        print "Sorry, must install the maybe package to run this program."
        sys.exit(1)
    

提交回复
热议问题