Created
August 23, 2021 14:15
-
-
Save hoffrocket/5cc95e875e9fd05073c5f2f320ce1f3b to your computer and use it in GitHub Desktop.
Get licenses for all python packages in environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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