-
-
Save orendon/292a2f3abbb7fa85197f62677de7428e to your computer and use it in GitHub Desktop.
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 |
because you arent using linux debian
@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.
https://notes.shanakadesoysa.com/Python/Basics/pip_package_size/
This works on Mac with gawk
installed
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
Dosen't work for me, I'm using M1 arch
no such option: ------------------