user402250
user402250

Reputation:

Check if a Debian package is installed from Python

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?

Upvotes: 20

Views: 17224

Answers (8)

v1s1t0r999
v1s1t0r999

Reputation: 1

I had the same doubt. Searched every corner in the Internet but couldn't find it. But finally after some Experiments I DID IT!!.

  • Code:
  1. import os
  2. packagename = "figlet" # Type in your package name
  3. os.system("dpkg -s "+packagename" | grep Status")

To type in any terminal using python codes:

  • Code:
  1. import os
  2. os.system("YOUR TERMINAL COMMAND HERE")

Upvotes: 0

zachfrey
zachfrey

Reputation: 63

Inspired by the previous answers, this works nicely for both Python 2 and Python 3 and avoids try/catch for the key error:

import apt
package = 'foo' # insert your package name here
cache = apt.Cache()
package_installed = False

if package in cache:
    package_installed = cache[package].is_installed

Upvotes: 0

user5074403
user5074403

Reputation:

This is some code that would give you a neat way to display if the package is installed or not (without triggering a messy error message on the screen). This works in Python 3 only, though.

import apt
cache = apt.Cache()
cache.open()

response = "Package Installed."
try:
    cache['notapkg'].is_installed
except KeyError:
    response = "Package Not Installed."

print(response)

Upvotes: 2

Poutsi
Poutsi

Reputation: 13

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.

Upvotes: 1

maxadamo
maxadamo

Reputation: 534

This is a pythonic way:

import apt
cache = apt.Cache()
if cache['package-name'].is_installed:
    print "YES it's installed"
else:
    print "NO it's NOT installed"

Upvotes: 26

loevborg
loevborg

Reputation: 1792

A slightly nicer, hopefully idiomatic version of your bash example:

import os, subprocess
devnull = open(os.devnull,"w")
retval = subprocess.call(["dpkg","-s","coreutils"],stdout=devnull,stderr=subprocess.STDOUT)
devnull.close()
if retval != 0:
    print "Package coreutils not installed."

Upvotes: 8

PaulMcG
PaulMcG

Reputation: 63739

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)

Upvotes: 2

Oli
Oli

Reputation: 239850

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.

Upvotes: 1

Related Questions