Wednesday 25 July 2018

Check dependency in python

import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
def checkHealth():
try:
text_file = open("requirements.txt", "r")
lines = text_file.readlines()
text_file.close()
# dependencies can be any iterable with strings,
# e.g. file line-by-line iterator
dependencies = lines

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is thrown.
pkg_resources.require(dependencies)
return True
except Exception as e:
print(e)
return False