How do I check whether a module is installed in Python, and install it if needed?

How do I check whether a module is installed in Python, and install it if needed?


PKGS=['selenium','ctypes','time', 'os','shutil','subprocess','unittest','urllib']

for p in PKGS:
import imp
m_repr = "'%s'"%p
m_repr = m_repr.ljust(20,".")
print("Module %s"%m_repr,)
try:
fp, pathname, description = imp.find_module(p)
imp.load_module(p, fp, pathname, description)
except:
print("FAIL")
continue
finally:
print("OK")

Loading Facebook Comments ...