Skip to content

Instantly share code, notes, and snippets.

@andreif
Last active March 14, 2023 23:43
Show Gist options
  • Select an option

  • Save andreif/316226a50feb2bdb03769f423a67d452 to your computer and use it in GitHub Desktop.

Select an option

Save andreif/316226a50feb2bdb03769f423a67d452 to your computer and use it in GitHub Desktop.
import pkg_resources
installed_dists = {dist.project_name: dist
for dist in pkg_resources.working_set}
def get_installed(project_name):
for dist in pkg_resources.working_set:
if project_name.lower() == dist.project_name.lower():
return dist
def print_tree(reqs, prefix='', last=False):
for i, req in enumerate(reqs):
last = i == len(reqs) - 1
tr = prefix + (' └─' if last else ' ├─')
if isinstance(req, pkg_resources.Distribution):
_req = f'{req.project_name}=={req.version}'
else:
_req = req
dist = get_installed(req.project_name)
if dist:
_dist = f'{dist.project_name}=={dist.version}'
if prefix and dist not in req:
print(f'{tr}🛑 {_req} ({_dist})')
else:
print(f'{tr}✅ {_req} ({_dist})')
print_tree(dist.requires(),
prefix=prefix + (' ' if last else ' │'))
else:
print(f'{tr}❌ {_req}')
print_tree(list(pkg_resources.working_set))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment