Skip to content

Instantly share code, notes, and snippets.

@akx
Created June 2, 2015 05:50
Show Gist options
  • Select an option

  • Save akx/a9654e0abb2bc41231f5 to your computer and use it in GitHub Desktop.

Select an option

Save akx/a9654e0abb2bc41231f5 to your computer and use it in GitHub Desktop.
Get installed package licenses in the current virtualenv
# -*- coding: utf-8 -*-
from pip import get_installed_distributions
def process_dist(dist):
license = None
metadata = None
if dist.has_metadata('PKG-INFO'):
metadata = dist.get_metadata('PKG-INFO')
if dist.has_metadata('METADATA'):
metadata = dist.get_metadata('METADATA')
if metadata:
license = find_license(metadata)
yield (dist.project_name, license or "<no license found>")
def find_license(metadata):
for line in metadata.splitlines():
if line.startswith("License:"):
license = line.split(None, 1)[-1]
if license and "unknown" not in license.lower():
return license
if line.startswith("Classifier:"):
classifier = line.split(" ", 1)[-1]
if classifier.startswith("License"):
return classifier.split(" :: ")[-1]
def run():
licenses = {}
for dist in get_installed_distributions(local_only=True):
licenses.update(process_dist(dist))
for project_name, version in sorted(licenses.items()):
print("%s\t%s" % (project_name, version))
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment