I wrote a script (kind of a learning thing) to open .rar archived files using unrar (non-free) in Linux. Now I want to add a section where my script will check to see if unrar is installed. To complicate matters, Linux also has a package 'unrar-free', and both use the name 'unrar' (therefore are incompatible with each other). So now, I not only need to see if 'unrar' is installed, I need to make sure it's the non-free version .

How would I check for a simple dependency at run-time?

You should be able to do this by invoking a shell command, which depends on your linux distribution, to list the packages installed on your computer. This command should look like rmp -qa | grep unrar on some distributions or dpkg -list unrar on other distributions and perhaps another command on your distribution. Check which command applies to your distro.
Once you have the shell command, you can use the subprocess module to run the command and check it's output, like this

import subprocess as sp
command = "rpm -qa | grep unrar"
child = sp.Popen(command, shell = True, stdout = sp.PIPE, stderr = sp.PIPE)
output, error = child.communicate()
if child.returncode:
    # command failed
    raise RuntimeError(error)
else:
    # Success. The string 'output' contains your command's output
    draw_conclusions_from_command_output(output)

Cool. That works. For others with the same problem, I use Ubuntu and therefore 'dpkg -l unrar'. dpkg doesn't return a code so I watched for the reply "No packages found matching unrar". Other than that, this is what I needed. Thanks you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.