如何查看安装的 pip 包大小?
How to see pip package sizes installed?
我不确定这是否可能。 Google 似乎没有任何答案。
运行 Linux Debian 我可以列出所有安装的 pip 包和大小(使用的磁盘数量 space)吗?
即列出所有 pip
个磁盘大小的包?
前往包裹网站查找尺码,例如https://pypi.python.org/pypi/pip/json
然后展开releases
,找到版本,然后查找size
(以字节为单位)。
可以试试这个吗(虽然有点长,也许有更好的解决方案):
$ pip list | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null
输出应如下所示:
80K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/blinker
3.8M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/docutils
296K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/ecdsa
340K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/execnet
564K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/fabric
1.4M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/flask
316K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/httplib2
1.9M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/jinja2
...
如果软件包安装在 Location/Name
中,应该可以使用。 (位置和名称来自pip show <package>
)
pip show <package>
将显示位置:
---
Metadata-Version: 2.0
Name: Flask
Version: 0.10.1
Summary: A microframework based on Werkzeug, Jinja2 and good intentions
Home-page: http://github.com/mitsuhiko/flask/
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD
Location: /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages
Requires: itsdangerous, Werkzeug, Jinja2
我们把Name
和Location
连起来得到位置,最后用du -sh
得到包大小
历史:
目前没有为此目的开发的命令或应用程序,我们需要手动检查
手动方法一:
du /usr/lib/python3.5/ --max-depth=2 | sort -h
du /usr/lib64/python3.5/ --max-depth=2 | sort -h
这不包括从该目录安装的 packages/files,因此说我们将使用这 2 个简单命令
获得 95%
另外如果你安装了其他版本的python,你需要调整目录
手动方法二:
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" $(find -maxdepth 1 -iname )}' | xargs du -sh | sort -h
使用不区分大小写的包名搜索安装目录
手动方法 II 备选方案 I :
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - -| awk '{print "/" tolower()}' | xargs du -sh | sort -h
使用小写的包名搜索安装目录
手动方法二备选方案二:
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - -| awk '{print "/" }' | xargs du -sh | sort -h
搜索包名的安装目录
注:
对于使用du
的方法,需要手动检查以du: cannot access
开头的输出行;
该命令使用安装目录并将包名称添加到其中,但有时包名称和目录名称不同...
简单点:
- 然后使用第一种方法
- 使用第二种方法,只需手动检查 python 经典目录外的包
新点列表格式的新版本:
pip2 list --format freeze|awk -F = {'print '}| xargs pip2 show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null|sort -h
针对 pip 版本 18 及更高版本进行了修改:
pip list | tail -n +3 | awk '{print }' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -hr
此命令显示 pip 包,按大小降序排列。
方法如下,
pip3 show numpy | grep "Location:"
- 这将 return path/to/all/packages
du -h path/to/all/packages
- 最后一行将包含以 MB 为单位的所有包的大小
注意:您可以使用任何包名称代替 numpy
以上所有解决方案均未列出其中包含 破折号 的包:PIP 将它们转换为文件夹名称中的下划线:
pip list --format freeze | awk -F = {'print '} | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{gsub("-","_",); print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -h
Mac 用户:
pip3 list --format freeze | awk -F = {'print '} | xargs pip3 show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{gsub("-","_",); print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -h
如何
$ du -h -d 1 "$(pip -V | cut -d ' ' -f 4 | sed 's/pip//g')" | grep -vE "dist-info|_distutils_hack|__pycache__" | sort -h
优点
无需转换这些:
案例 (Django:django)
连字符 (django-q:django_q)
命名 (djangorestframework-gis:rest_framework_gis)
缺点
依赖项和一些未知目录也显示...
虽然有一种简单的 Pythonic 方法可以找到它。
这是代码。我们称这个文件为 pipsize.py
.
import os
import pkg_resources
def calc_container(path):
total_size = 0
for dirpath, dirnames, 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]
for dist in dists:
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
if size/1000 > 1.0:
print (f"{dist}: {size/1000} KB")
print("-"*40)
except OSError:
'{} no longer exists'.format(dist.project_name)
当 运行 和 python pipsize.py
时,这将打印出类似
的内容
pip 21.1.2: 8651.906 KB
----------------------------------------
numpy 1.20.3: 25892.871 KB
----------------------------------------
numexpr 2.7.3: 1627.361 KB
----------------------------------------
zict 2.0.0: 48.54 KB
----------------------------------------
yarl 1.6.3: 1395.888 KB
----------------------------------------
widgetsnbextension 3.5.1: 4609.962 KB
----------------------------------------
webencodings 0.5.1: 54.768 KB
----------------------------------------
wcwidth 0.2.5: 452.214 KB
----------------------------------------
uvicorn 0.14.0: 257.515 KB
----------------------------------------
tzlocal 2.1: 67.11 KB
----------------------------------------
traitlets 5.0.5: 800.71 KB
----------------------------------------
tqdm 4.61.0: 289.412 KB
----------------------------------------
tornado 6.1: 2898.264 KB
您可以 运行 第 1 部分自行获取所有当前包 python tool-size.py
将为您汇总它们
如果你想知道特定 pip 包的确切大小,包括它的所有依赖项,我创建了一些 bash 和 python 组合来实现这个
(基于上面的优秀包裹步行代码答案)
步骤:
- 创建一个 python 脚本来检查所有当前安装的 pip 包
- 创建一个 shell 脚本来创建一个全新的 python 环境并安装包进行测试,运行 来自第 1 步的脚本
- 运行 shell 脚本
- 利润:)
第 1 步
创建一个名为 tool-size.py
的 python 脚本
#!/usr/bin/env python
import os
import pkg_resources
def calc_container(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
def calc_installed_sizes():
dists = [d for d in pkg_resources.working_set]
total_size = 0
print (f"Size of Dependencies")
print("-"*40)
for dist in dists:
# ignore pre-installed pip and setuptools
if dist.project_name in ["pip", "setuptools"]:
continue
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
total_size += size
if size/1000 > 1.0:
print (f"{dist}: {size/1000} KB")
print("-"*40)
except OSError:
'{} no longer exists'.format(dist.project_name)
print (f"Total Size (including dependencies): {total_size/1000} KB")
if __name__ == "__main__":
calc_installed_sizes()
第 2 步
创建一个名为 tool-size.sh
的 bash 脚本
#!/usr/bin/env bash
# uncomment to to debug
# set -x
rm -rf ~/.virtualenvs/tool-size-tester
python -m venv ~/.virtualenvs/tool-size-tester
source ~/.virtualenvs/tool-size-tester/Scripts/activate
pip install -q
python tool-size.py
deactivate
步骤 3
运行 包含您想要获得大小的包的脚本
tool-size.sh xxx
说 truffleHog3
$ ./tool-size.sh truffleHog3
Size of Dependencies
----------------------------------------
truffleHog3 2.0.6: 56.46 KB
----------------------------------------
smmap 4.0.0: 108.808 KB
----------------------------------------
MarkupSafe 2.0.1: 40.911 KB
----------------------------------------
Jinja2 3.0.1: 917.551 KB
----------------------------------------
gitdb 4.0.7: 320.08 KB
----------------------------------------
Total Size (including dependencies): 1443.81 KB
我不确定这是否可能。 Google 似乎没有任何答案。
运行 Linux Debian 我可以列出所有安装的 pip 包和大小(使用的磁盘数量 space)吗?
即列出所有 pip
个磁盘大小的包?
前往包裹网站查找尺码,例如https://pypi.python.org/pypi/pip/json
然后展开releases
,找到版本,然后查找size
(以字节为单位)。
可以试试这个吗(虽然有点长,也许有更好的解决方案):
$ pip list | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null
输出应如下所示:
80K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/blinker
3.8M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/docutils
296K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/ecdsa
340K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/execnet
564K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/fabric
1.4M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/flask
316K /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/httplib2
1.9M /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages/jinja2
...
如果软件包安装在 Location/Name
中,应该可以使用。 (位置和名称来自pip show <package>
)
pip show <package>
将显示位置:
---
Metadata-Version: 2.0
Name: Flask
Version: 0.10.1
Summary: A microframework based on Werkzeug, Jinja2 and good intentions
Home-page: http://github.com/mitsuhiko/flask/
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD
Location: /home/lord63/.pyenv/versions/2.7.11/envs/py2/lib/python2.7/site-packages
Requires: itsdangerous, Werkzeug, Jinja2
我们把Name
和Location
连起来得到位置,最后用du -sh
得到包大小
历史:
目前没有为此目的开发的命令或应用程序,我们需要手动检查
手动方法一:
du /usr/lib/python3.5/ --max-depth=2 | sort -h
du /usr/lib64/python3.5/ --max-depth=2 | sort -h
这不包括从该目录安装的 packages/files,因此说我们将使用这 2 个简单命令
获得 95%另外如果你安装了其他版本的python,你需要调整目录
手动方法二:
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" $(find -maxdepth 1 -iname )}' | xargs du -sh | sort -h
使用不区分大小写的包名搜索安装目录
手动方法 II 备选方案 I :
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - -| awk '{print "/" tolower()}' | xargs du -sh | sort -h
使用小写的包名搜索安装目录
手动方法二备选方案二:
pip list | sed '/Package/d' | sed '/----/d' | sed -r 's/\S+//2' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - -| awk '{print "/" }' | xargs du -sh | sort -h
搜索包名的安装目录
注:
对于使用du
的方法,需要手动检查以du: cannot access
开头的输出行;
该命令使用安装目录并将包名称添加到其中,但有时包名称和目录名称不同...
简单点:
- 然后使用第一种方法
- 使用第二种方法,只需手动检查 python 经典目录外的包
新点列表格式的新版本:
pip2 list --format freeze|awk -F = {'print '}| xargs pip2 show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null|sort -h
针对 pip 版本 18 及更高版本进行了修改:
pip list | tail -n +3 | awk '{print }' | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -hr
此命令显示 pip 包,按大小降序排列。
方法如下,
pip3 show numpy | grep "Location:"
- 这将 return path/to/all/packages
du -h path/to/all/packages
- 最后一行将包含以 MB 为单位的所有包的大小
注意:您可以使用任何包名称代替 numpy
以上所有解决方案均未列出其中包含 破折号 的包:PIP 将它们转换为文件夹名称中的下划线:
pip list --format freeze | awk -F = {'print '} | xargs pip show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{gsub("-","_",); print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -h
Mac 用户:
pip3 list --format freeze | awk -F = {'print '} | xargs pip3 show | grep -E 'Location:|Name:' | cut -d ' ' -f 2 | paste -d ' ' - - | awk '{gsub("-","_",); print "/" tolower()}' | xargs du -sh 2> /dev/null | sort -h
如何
$ du -h -d 1 "$(pip -V | cut -d ' ' -f 4 | sed 's/pip//g')" | grep -vE "dist-info|_distutils_hack|__pycache__" | sort -h
优点
无需转换这些:
案例 (Django:django)
连字符 (django-q:django_q)
命名 (djangorestframework-gis:rest_framework_gis)
缺点
依赖项和一些未知目录也显示...
虽然有一种简单的 Pythonic 方法可以找到它。
这是代码。我们称这个文件为 pipsize.py
.
import os
import pkg_resources
def calc_container(path):
total_size = 0
for dirpath, dirnames, 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]
for dist in dists:
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
if size/1000 > 1.0:
print (f"{dist}: {size/1000} KB")
print("-"*40)
except OSError:
'{} no longer exists'.format(dist.project_name)
当 运行 和 python pipsize.py
时,这将打印出类似
pip 21.1.2: 8651.906 KB
----------------------------------------
numpy 1.20.3: 25892.871 KB
----------------------------------------
numexpr 2.7.3: 1627.361 KB
----------------------------------------
zict 2.0.0: 48.54 KB
----------------------------------------
yarl 1.6.3: 1395.888 KB
----------------------------------------
widgetsnbextension 3.5.1: 4609.962 KB
----------------------------------------
webencodings 0.5.1: 54.768 KB
----------------------------------------
wcwidth 0.2.5: 452.214 KB
----------------------------------------
uvicorn 0.14.0: 257.515 KB
----------------------------------------
tzlocal 2.1: 67.11 KB
----------------------------------------
traitlets 5.0.5: 800.71 KB
----------------------------------------
tqdm 4.61.0: 289.412 KB
----------------------------------------
tornado 6.1: 2898.264 KB
您可以 运行 第 1 部分自行获取所有当前包 python tool-size.py
将为您汇总它们
如果你想知道特定 pip 包的确切大小,包括它的所有依赖项,我创建了一些 bash 和 python 组合来实现这个
(基于上面的优秀包裹步行代码答案
步骤:
- 创建一个 python 脚本来检查所有当前安装的 pip 包
- 创建一个 shell 脚本来创建一个全新的 python 环境并安装包进行测试,运行 来自第 1 步的脚本
- 运行 shell 脚本
- 利润:)
第 1 步
创建一个名为 tool-size.py
#!/usr/bin/env python
import os
import pkg_resources
def calc_container(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
def calc_installed_sizes():
dists = [d for d in pkg_resources.working_set]
total_size = 0
print (f"Size of Dependencies")
print("-"*40)
for dist in dists:
# ignore pre-installed pip and setuptools
if dist.project_name in ["pip", "setuptools"]:
continue
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
total_size += size
if size/1000 > 1.0:
print (f"{dist}: {size/1000} KB")
print("-"*40)
except OSError:
'{} no longer exists'.format(dist.project_name)
print (f"Total Size (including dependencies): {total_size/1000} KB")
if __name__ == "__main__":
calc_installed_sizes()
第 2 步
创建一个名为 tool-size.sh
#!/usr/bin/env bash
# uncomment to to debug
# set -x
rm -rf ~/.virtualenvs/tool-size-tester
python -m venv ~/.virtualenvs/tool-size-tester
source ~/.virtualenvs/tool-size-tester/Scripts/activate
pip install -q
python tool-size.py
deactivate
步骤 3
运行 包含您想要获得大小的包的脚本
tool-size.sh xxx
说 truffleHog3
$ ./tool-size.sh truffleHog3
Size of Dependencies
----------------------------------------
truffleHog3 2.0.6: 56.46 KB
----------------------------------------
smmap 4.0.0: 108.808 KB
----------------------------------------
MarkupSafe 2.0.1: 40.911 KB
----------------------------------------
Jinja2 3.0.1: 917.551 KB
----------------------------------------
gitdb 4.0.7: 320.08 KB
----------------------------------------
Total Size (including dependencies): 1443.81 KB