Skip to content

Instantly share code, notes, and snippets.

@melizeche
Created February 9, 2018 21:28
Show Gist options
  • Save melizeche/387f9b2ee05738e175b60d4a8c1fa6d4 to your computer and use it in GitHub Desktop.
Save melizeche/387f9b2ee05738e175b60d4a8c1fa6d4 to your computer and use it in GitHub Desktop.
Get licenses of all packages in use
import pkg_resources
import prettytable
def get_pkg_license(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except:
lines = pkg.get_metadata_lines('PKG-INFO')
for line in lines:
if line.startswith('License:'):
return line[9:]
return '(Licence not found)'
def print_packages_and_licenses():
t = prettytable.PrettyTable(['Library', 'License'])
for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()):
t.add_row((str(pkg), get_pkg_license(pkg)))
print(t)
if __name__ == "__main__":
print_packages_and_licenses()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment