Skip to content

Instantly share code, notes, and snippets.

@mdboom
Created November 12, 2019 19:05
Show Gist options
  • Save mdboom/52194b2fbafb5548f1cde9d9965c8344 to your computer and use it in GitHub Desktop.
Save mdboom/52194b2fbafb5548f1cde9d9965c8344 to your computer and use it in GitHub Desktop.
Measures the amount of code loaded by "import X"
import os
import sys
original_modules = dict(sys.modules)
import pandas
sizes = {}
total = 0
files = set()
for name, module in sys.modules.items():
if name in original_modules:
continue
if hasattr(module, "__file__"):
filename = module.__file__
files.add(filename)
for filename in files:
stat = os.stat(filename)
total += stat.st_size
for package in ["pandas", "numpy", "matplotlib", "dateutil"]:
if f"site-packages/{package}/" in filename:
sizes.setdefault(package, 0)
sizes[package] += stat.st_size
break
else:
sizes.setdefault("stdlib", 0)
sizes["stdlib"] += stat.st_size
print(total)
print(sizes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment