Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created March 21, 2023 09:48
Show Gist options
  • Save bitsnaps/2402619cf7b39cfc42b109f7ae19cc01 to your computer and use it in GitHub Desktop.
Save bitsnaps/2402619cf7b39cfc42b109f7ae19cc01 to your computer and use it in GitHub Desktop.
A script to browse all python packages and show their sizes
#!/usr/bin/env python
import os
import pkg_resources
def calc_container(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
def calc_installed_sizes():
dists = [d for d in pkg_resources.working_set]
total_size = 0
print (f"Size of Dependencies")
#print("-"*40)
for dist in dists:
# ignore pre-installed pip and setuptools
if dist.project_name in ["pip", "setuptools"]:
continue
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
total_size += size
deps = [str(r) for r in dist.requires()]
if size/1000 > 1.0:
print (f"{dist}: {size/1000} Kb | deps ({len(deps)}): {deps}")
print("-"*40)
except OSError:
'{} no longer exists'.format(dist.project_name)
print (f"Total Size (including dependencies): {total_size/(1000**2)} Mb")
if __name__ == "__main__":
calc_installed_sizes()
# Inspired from: https://stackoverflow.com/questions/24660570/how-to-determine-the-size-of-a-non-installed-python-package
# It is better to use a dedicated tool like pipdeptree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment