当我使用pip install module_name
安装模块时,我能够看到任何一个轮子或包裹的大小。
所以问题是,是否可以只知道每个模块的大小?
像这样的东西。import pip
for dist in pip.get_installed_distributions():
print(distribution_size_only of dist)
我想知道分发的估计大小,以便我可以从winpython发行版中删除它们。
答案 0 :(得分:1)
import os
import pip
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
for dist in pip.get_installed_distributions():
try:
path = os.path.join(dist.location, dist.project_name)
size = calc_container(path)
if size:
print path
print size
except OSError:
'{} no longer exists'.format(dist.project_name)
如果您使用virtualenv,则可以使用第一个选项获取更多信息:
get_installed_distributions(local_only = True,skip =(' python',' wsgiref',' argparse'),include_editables = True,editables_only = False,user_only =假) 返回已安装的分发对象列表。
如果
local_only
为True(默认值),则仅返回安装 当前的virtualenv本地,如果在virtualenv。
答案 1 :(得分:0)
我知道这很模糊,但这就是我想要的:
import re
import requests
import json
def regexsubstring(s, p):
p = re.compile(p, flags=re.IGNORECASE)
return p.search(s)
def wf(data,path,mode):
with open(path, mode, encoding='utf-8') as out:
out.write(data)
return
import pip
dists_size_info = {}
for dist in pip.get_installed_distributions():
url = "https://pypi.python.org/pypi/" + dist.key + "/" + dist.version
r = requests.get(url)
size = regexsubstring(r.text, """<td style="text-align: right;">(\w+)</td>""").group(1)
dists_size_info[dist.key] = [dist.version, size]
print(dists_size_info)
wf(json.dumps(dists_size_info),'dists_size_info.txt','w')
或者您可以在以下网址获取列表: