David542 :
I would like to make sure that the user has mysql installed in order to run a script. Normally I would type in $ mysql
in the shell and if it goes to the mysql>
prompt I know I'm good. However, how would I do this from within a python script? Something like:
try:
assert (0 == subprocess.call("mysql -u root -e 'show databases'", shell=True, stderr=subprocess.DEVNULL))
except AssertionError:
print ("You need to install mysql to run this script")\
What would be the proper way to do this?
Kyle Laker :
You can use shutil.which
. This is available in Python 3.3+ and will use the same PATH lookup as the operating system you are runnig on.
from shutil import which
def mysql_exists():
"""
Verifies that the MySQL client is available. Returns true if it exists,
and false if it does not.
"""
return which('mysql') is not None