Linux – Bash scripting: Crossplatform check is python package installed in the system

bashlinuxunix

For example I want to check does py-sqlite3 available..? One of the methods would to call that command with some minimum python script and catch error?

I want to make check for any linux distro and unix system (at least bsd)

What could be the best way to achieve this?

P.s. Please provide example of shell scripting error catching, because I'm not so advanced in shell scripting

Best Answer

I have found existing answers incomplete and lacking good enough examples. Here is the solution I have settled on:

# an example checking if the pandas package is installed
if python -c 'import pkgutil; exit(not pkgutil.find_loader("pandas"))'; then
    echo 'pandas found'
else
    echo 'pandas not found'
fi

A Github gist of this example can be found here: https://gist.github.com/shaypal5/d505af9953cd86f59c750fa600ee4ba6