Skip to content

Instantly share code, notes, and snippets.

@hoffrocket
Created August 23, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffrocket/5cc95e875e9fd05073c5f2f320ce1f3b to your computer and use it in GitHub Desktop.
Save hoffrocket/5cc95e875e9fd05073c5f2f320ce1f3b to your computer and use it in GitHub Desktop.
Get licenses for all python packages in environment
import pkg_resources
import sys
def get_pkg_license(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except Exception:
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(name):
for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()):
print(name, "\t", str(pkg), "\t", get_pkg_license(pkg))
if __name__ == "__main__":
print_packages_and_licenses(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment