Created
January 10, 2018 20:36
-
-
Save orendon/292a2f3abbb7fa85197f62677de7428e to your computer and use it in GitHub Desktop.
Python: list pip packages size
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
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 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@saurbhc it didn't work for me on WSL Ubuntu 18.04 either; the problem is
pip list
has a header line of dashes whichxargs
tries to read as an option.The rest of the pipeline works for a given package name:
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.