Bash scripting: Crossplatform check is python package installed in the system? – A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about Bash scripting: Crossplatform check is python package installed in the system? was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, unix, bash, , .
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
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
In bash:
$ python -c 'import sqlite3' 2>/dev/null && echo "python sqlite3 modules install" || echo "python sqlite3 modules not install"
python sqlite3 modules install
$ python -c 'import sqlite3' 2>/dev/null && echo "python sqlite3 modules install" || echo "python sqlite3 modules not install"
python sqlite3 modules not install
package_exist(){
package=$1
if pip freeze | grep $package=; then
echo "$package found"
else
echo "$package not found"
fi
}
and then you can use it like this:
package_exist package_name
This would be easy to do with Python.
import sys
try:
import sqlite3
except ImportError:
sys.exit(1)
else:
sys.exit(0)
Exits with return code 0
if it can import it, or return code 1
if it cannot.
Andrew