1#!/usr/bin/env python 2 3from pathlib import Path 4import pkg_resources 5import re 6 7 8def main(): 9 requirements_dir = Path(__file__).parent / '..' / 'requirements' 10 11 for requirement_file in requirements_dir.glob('*.txt'): 12 print(requirement_file.name) 13 with open(str(requirement_file), 'r') as f: 14 for req in f: 15 # Remove trailing and leading whitespace. 16 req = req.strip() 17 18 if not req: 19 # skip empty or white space only lines 20 continue 21 elif req.startswith('#'): 22 continue 23 24 # Get the name of the package 25 req = re.split('<|>|=|!|;', req)[0] 26 try: 27 # use pkg_resources to reliably get the version at install 28 # time by package name. pkg_resources needs the name of the 29 # package from pip, and not "import". 30 # e.g. req is 'scikit-learn', not 'sklearn' 31 version = pkg_resources.get_distribution(req).version 32 print(req.rjust(20), version) 33 except pkg_resources.DistributionNotFound: 34 print(req.rjust(20), 'is not installed') 35 36 37if __name__ == '__main__': 38 main() 39