Created
March 5, 2022 04:20
-
-
Save AnsonH/fd634ba4298376f2abd8e00f99b01be8 to your computer and use it in GitHub Desktop.
Lists size of pip packages
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
# Run `python pipsize.py` in Terminal to show size of pip packages | |
# Credits: https://stackoverflow.com/a/67914559/11067496 | |
sort_in_descending = True # Show packages in descending order | |
import os | |
import pkg_resources | |
def calc_container(path): | |
total_size = 0 | |
for dirpath, _, filenames in os.walk(path): | |
for f in filenames: | |
fp = os.path.join(dirpath, f) | |
total_size += os.path.getsize(fp) | |
return total_size | |
dists = [d for d in pkg_resources.working_set] | |
dists_with_size = {} | |
for dist in dists: | |
try: | |
path = os.path.join(dist.location, dist.project_name) | |
size = calc_container(path) | |
dists_with_size[size] = dist | |
except OSError: | |
'{} no longer exists'.format(dist.project_name) | |
# Sort packages size | |
dists_with_size = dict(sorted(dists_with_size.items(), reverse=sort_in_descending)) | |
for size, dist in dists_with_size.items(): | |
if size/1000 > 1.0: | |
print (f"{dist}: {size/1000000:.2f} MB") | |
print("-"*40) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is my improved version with line showing the total space, a line showing the space taken by all the small libraries, and a table-like formatting to display everything.
It outputs: