Alex Rivera | Logout

Check if a Debian package is installed from Python

Asked 2010-08-02T12:58:35.523
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 Status

Suggestions to do the same in a Python script?

Edit
Report

2 Answers

1

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)
answered 2010-08-02T13:34:16.400
0

Have a look at commands. It's very useful for running things on the command line and getting the status.

Otherwise, I'm sure there is some library that will let you interact with apt. python-apt might work but it's a bit raw. Just capturing the command line seems easier.

answered 2010-08-02T13:10:53.593

Your Answer