Skip to content

Instantly share code, notes, and snippets.

@orendon
Created January 10, 2018 20:36
Show Gist options
  • Save orendon/292a2f3abbb7fa85197f62677de7428e to your computer and use it in GitHub Desktop.
Save orendon/292a2f3abbb7fa85197f62677de7428e to your computer and use it in GitHub Desktop.
Python: list pip packages size
pip list | xargs pip show | grep -E 'Location|Name' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print $2 "/" tolower($1)}' | xargs du -sh 2> /dev/null
@saurbhc
Copy link

saurbhc commented Aug 31, 2021

Dosen't work for me, I'm using M1 arch
no such option: ------------------

@dipanshuhappy
Copy link

because you arent using linux debian

@icedwater
Copy link

@saurbhc it didn't work for me on WSL Ubuntu 18.04 either; the problem is pip list has a header line of dashes which xargs tries to read as an option.

The rest of the pipeline works for a given package name:

pip show <packagename> \
| grep -E 'Location|Name' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print $2 "/" tolower($1)}' \
| xargs du -sh

Line 1 finds the package, line 2 puts the package name behind its path, and line 3 checks the disk usage of that subdirectory.

Maybe when @orendon posted this, pip list gave different output.

@liuyigh
Copy link

liuyigh commented Jan 14, 2023

@dannysauer
Copy link

dannysauer commented May 16, 2023

It'd probably be safer to use one of the other list formats which are intended to be machine-parseable:

pip list --format json | jq -r '.[]|.name' | xargs pip show...

or

pip list --format freeze | cut -d= -f1 | xargs pip show...

Personally, I'd just do this as the entire solution, if I was on a system with GNU awk (so a Mac user or Solaris or whatever might need to use gawk instead of awk; I forget if tolower is standard awk or a GNU extension):

pip list --format freeze | cut -d= -f1 | \
  while read P; do du -hs $( pip show $P | awk -v p="$P" '/Location/{print $2"/"tolower(p)}' ); done

That gives this output on a python 3.9 container with pygments installed. It still fails for some eggs, but gets you close.

13M	/usr/local/lib/python3.9/site-packages/pip
7.9M	/usr/local/lib/python3.9/site-packages/pygments
2.3M	/usr/local/lib/python3.9/site-packages/setuptools
264K	/usr/local/lib/python3.9/site-packages/wheel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment