Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Within my python3 software I need to check to see if git is installed. I know that from the terminal I can type:

dpkg --get-selections > filename

to find what programs are installed and then put that into a file.

From there, how would I get python to see if git is installed? Or is there an easier way?

share|improve this question
    
Using the terminal command and putting it into a file doesn't work, as if you uninstall a program the file is still there but just has the word "deinstalled" on the same line instead of "installed". –  Eden Crow Apr 18 '11 at 12:36
    
To stick with a terminal command but do it a lot easier, you can do: "dpkg -s git". You'll get the package details if it's installed or "Package `git' is not installed and no info is available" if it's not installed. –  Alin Andrei Apr 18 '11 at 13:51

5 Answers 5

You may use the python apt module, but it only checks if the package is installed. It will fail non-deb distributions or if installed outside the package manager:

import apt
cache = apt.Cache()
cache.open()
 cache["git-core"].is_installed# Evaluates true if git is installed

Try to run git and handle the exception if it fails:

import subprocess

try:
    # pipe output to /dev/null for silence
    null = open("/dev/null", "w")
    subprocess.Popen("git", stdout=null, stderr=null)
    null.close()

except OSError:
    print("git not found")

A third option would be to try all the paths in $PATH and test if you have exec permission on path/git.

share|improve this answer

Look for installed files

Assuming we know where the application's launcher or binary is installed by default an easy to do approach would be to just check for it's existence like:

#! /usr/bin/env python3
#
# Query package state by installation path

import os.path
path = '/usr/bin/git'


def query_package(path):
    if os.path.exists(path):
        print('Git is ready to use')
    else:
        print('please install Git')

Consult DPKG

Another way to query installed packages however is to consult the package management, in our case dpkg:

#! /usr/bin/env python3
#
# Query package state by dpkg status

import subprocess
package = 'git'

def query_package(package):
    status = subprocess.getstatusoutput("dpkg-query -W -f='${Status}' " + package)
    if not status[0]:
        print(status[1]) # package is installed
    else:
        print(status[1])
share|improve this answer

If you wanted to do it that way you could do something like:

checkvar = 0  
for line in open("filename"):  
    if "git" in line:  
        checkvar = checkvar + 1  
print "Phrase git found ", checkvar, " times"
share|improve this answer

I would take a different approach. In order to check whether Git is available on a given system, either check if the git binary exist in user's PATH, or just try to execute git --version. This check will be more reliable, and it supports people who (for any reason) install Git from source in /usr/local/bin, or even ~/bin.

This is not a direct answer to your question, but I hope it helps anyway. If you need a help in coding that in Python, let me know and I'll provide more details.

share|improve this answer
    
Seems reasonable, but how would I go about getting python to register whether or not git is actually there? –  Eden Crow Apr 19 '11 at 10:32
    
Try to run it with subprocess.Popen() and handle the exception if it fails. I have updated my answer. –  Egil Apr 19 '11 at 11:15
    
@Egil You're welcome :) –  Adam Byrtek Apr 19 '11 at 18:13

You could also try checking the output of which.

(Maybe that won't work as no one else has suggested it?)

share|improve this answer
    
which is a shell built-in, so you would have to spawn a shell first. –  Adam Byrtek Apr 19 '11 at 18:16
    
/usr/bin/which isn't a shell built-in, unless I'm mistaken. –  Broam Apr 21 '11 at 14:27
    
Right, it's a built-in only in Zsh (which I use). Sorry about that. –  Adam Byrtek Apr 21 '11 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.