Python - 获取已安装包的所有大小的总和

问题描述 投票:0回答:1

如何获取pythonrequirements.txt文件中所有包的大小总和?或者一个

pip freeze

我想算出所有包占用了多少空间。我知道 this 存在,但它没有显示如何获得总和。谢谢:D

python package
1个回答
0
投票

我很久以前就已经为我创建了一个 python 文件。即在看到你的问题之前。看到你的问题后很久才运行。

如果未安装,请安装 pipreqs 模块

pip3 install pipreqs

*installed_packages.py

import subprocess as sp
import re

def get_installed():
    # Get the list of installed packages
    op = sp.check_output(['pip', 'freeze']).decode('utf-8')
    ls = [line.split('==')[0] for line in op.splitlines()]
    return ls

def get_size(package):
    # Get package details
    try:
        op = sp.check_output(['pip', 'show', package]).decode('utf-8')
    except sp.CalledProcessError:
        return 0
    
    # Find the location of the package
    location_match = re.search(r'^Location: (.*)', op, re.MULTILINE)
    if location_match:
        location = location_match.group(1)
        
        # Get the size of the directory
        try:
            size_op = sp.check_output(['du', '-sb', location]).decode('utf-8')
            size = int(size_op.split()[0])
            return size
        except Exception as e:
            return 0
    return 0

def main():
    packages = get_installed()
    total = 0

    for x in packages:
        size = get_size(x)
        total = total + size
        print(f'{x}: {size / (1024 * 1024):.2f} MB')
    
    print(f'\nTotal size of all packages: {total / (1024 * 1024):.2f} MB')

if __name__ == '__main__':
    main()

我的输出:

annotated-types: 1018.58 MB
anyio: 1018.58 MB
apturl: 169.51 MB
argcomplete: 169.51 MB
argon2-cffi: 1018.58 MB
argon2-cffi-bindings: 1018.58 MB
arrow: 1018.58 MB
asttokens: 1018.58 MB
async-lru: 1018.58 MB
attrs: 1018.58 MB
avg: 1018.58 MB
Babel: 1018.58 MB
bcrypt: 169.51 MB
beautifulsoup4: 1018.58 MB
bleach: 1018.58 MB
blinker: 1018.58 MB
blis: 1018.58 MB
bottle: 1018.58 MB
Brlapi: 169.51 MB
bubblemail: 169.51 MB
catalogue: 1018.58 MB
certifi: 1018.58 MB
cffi: 1018.58 MB
chardet: 169.51 MB
charset-normalizer: 1018.58 MB
chrome-gnome-shell: 169.51 MB
click: 1018.58 MB
cloudpathlib: 1018.58 MB
colorama: 169.51 MB
comm: 1018.58 MB
command-not-found: 169.51 MB
confection: 1018.58 MB
contourpy: 1018.58 MB
cryptography: 169.51 MB
cupshelpers: 169.51 MB
customtkinter: 1018.58 MB
cycler: 1018.58 MB
cymem: 1018.58 MB
darkdetect: 1018.58 MB
dateparser: 1018.58 MB
dbus-python: 169.51 MB
debugpy: 1018.58 MB
decorator: 1018.58 MB
deepdiff: 1018.58 MB
defer: 169.51 MB
defusedxml: 1018.58 MB
distlib: 169.51 MB
distro: 169.51 MB
distro-info: 169.51 MB
dnspython: 169.51 MB
docopt: 1018.58 MB
duplicity: 169.51 MB
WARNING: Package(s) not found: en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl#sha256=86cc141f63942d4b2c5fcee06630fd6f904788d2f0ab005cce45aadb8fb73889
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl#sha256=86cc141f63942d4b2c5fcee06630fd6f904788d2f0ab005cce45aadb8fb73889: 0.00 MB
evdev: 1018.58 MB
example: 1018.58 MB
exceptiongroup: 1018.58 MB
executing: 1018.58 MB
fasteners: 169.51 MB
fastjsonschema: 1018.58 MB
filelock: 169.51 MB
Flask: 1018.58 MB
Flask-Cors: 1018.58 MB
Flask-Login: 1018.58 MB
Flask-MySQL: 1018.58 MB
flask-mysql-connector: 1018.58 MB
Flask-MySQLdb: 1018.58 MB
Flask-Reuploaded: 1018.58 MB
Flask-SQLAlchemy: 1018.58 MB
Flask-Uploads: 1018.58 MB
fonttools: 1018.58 MB
fpdf: 1018.58 MB
fqdn: 1018.58 MB
future: 169.51 MB
greenlet: 1018.58 MB
h11: 1018.58 MB
html5lib: 169.51 MB
httpcore: 1018.58 MB
httplib2: 169.51 MB
httpx: 1018.58 MB
idna: 169.51 MB
imageio: 1018.58 MB
imageio-ffmpeg: 1018.58 MB
importlib-metadata: 169.51 MB
install: 1018.58 MB
ipykernel: 1018.58 MB
ipython: 1018.58 MB
ipython_genutils: 169.51 MB
isoduration: 1018.58 MB
itsdangerous: 1018.58 MB
jedi: 1018.58 MB
jeepney: 169.51 MB
Jinja2: 1018.58 MB
jmespath: 1018.58 MB
joblib: 1018.58 MB
json5: 1018.58 MB
jsonpointer: 1018.58 MB
jsonschema: 1018.58 MB
jsonschema-specifications: 1018.58 MB
jupyter-events: 1018.58 MB
jupyter-lsp: 1018.58 MB
jupyter_client: 1018.58 MB
jupyter_core: 1018.58 MB
jupyter_server: 1018.58 MB
jupyter_server_terminals: 1018.58 MB
jupyterlab_pygments: 1018.58 MB
jupyterlab_server: 1018.58 MB
keyboard: 1018.58 MB
keyring: 169.51 MB
kiwisolver: 1018.58 MB
langcodes: 1018.58 MB
language-selector: 169.51 MB
launchpadlib: 169.51 MB
lazr.restfulclient: 169.51 MB
lazr.uri: 169.51 MB
lml: 1018.58 MB
lockfile: 169.51 MB
louis: 169.51 MB
lxml: 169.51 MB
macaroonbakery: 169.51 MB
Mako: 169.51 MB
MarkupSafe: 1018.58 MB
matplotlib: 1018.58 MB
matplotlib-inline: 1018.58 MB
mistune: 1018.58 MB
monotonic: 169.51 MB
more-itertools: 169.51 MB
MouseInfo: 1018.58 MB
murmurhash: 1018.58 MB
mysql-connector-python: 1018.58 MB
mysqlclient: 1018.58 MB
natsort: 169.51 MB
nbclient: 1018.58 MB
nbconvert: 1018.58 MB
nbformat: 1018.58 MB
nest-asyncio: 1018.58 MB
netifaces: 169.51 MB
nltk: 1018.58 MB
notebook_shim: 1018.58 MB
num2words: 1018.58 MB
numpy: 1018.58 MB
oauthlib: 169.51 MB
olefile: 169.51 MB
openai: 1018.58 MB
opencv-python: 1018.58 MB
ordered-set: 1018.58 MB
outcome: 1018.58 MB
overrides: 1018.58 MB
packaging: 1018.58 MB
pandas: 1018.58 MB
pandocfilters: 1018.58 MB
paramiko: 169.51 MB
parsedatetime: 1018.58 MB
parso: 1018.58 MB
passlib: 1018.58 MB
pathlib: 1018.58 MB
pexpect: 169.51 MB
Pillow: 1018.58 MB
platformdirs: 1018.58 MB
plum-dispatch: 1018.58 MB
polars: 1018.58 MB
preshed: 1018.58 MB
prometheus-client: 1018.58 MB
prompt-toolkit: 1018.58 MB
protobuf: 169.51 MB
proxy-tools: 1018.58 MB
psutil: 1018.58 MB
ptyprocess: 169.51 MB
pure-eval: 1018.58 MB
PyAutoGUI: 1018.58 MB
pycairo: 169.51 MB
pycparser: 1018.58 MB
pycups: 169.51 MB
pydantic: 1018.58 MB
pydantic_core: 1018.58 MB
pyexcel: 1018.58 MB
pyexcel-io: 1018.58 MB
PyGetWindow: 1018.58 MB
Pygments: 1018.58 MB
PyGObject: 169.51 MB
PyJWT: 169.51 MB
pymacaroons: 169.51 MB
PyMsgBox: 1018.58 MB
PyMySQL: 1018.58 MB
PyNaCl: 169.51 MB
pynput: 1018.58 MB
pyparsing: 169.51 MB
pyperclip: 1018.58 MB
PyRect: 1018.58 MB
pyRFC3339: 169.51 MB
PyScreeze: 1018.58 MB
PySocks: 1018.58 MB
python-apt: 169.51 MB
python-dateutil: 1018.58 MB
python-debian: 169.51 MB
python-docx: 1018.58 MB
python-dotenv: 1018.58 MB
python-json-logger: 1018.58 MB
python-pptx: 1018.58 MB
python-vlc: 1018.58 MB
python-xlib: 1018.58 MB
python3-xlib: 1018.58 MB
pythonping: 1018.58 MB
pytweening: 1018.58 MB
pytz: 169.51 MB
pywebview: 1018.58 MB
pyxdg: 169.51 MB
PyYAML: 169.51 MB
pyzmq: 1018.58 MB
referencing: 1018.58 MB
regex: 1018.58 MB
requests: 1018.58 MB
rfc3339-validator: 1018.58 MB
rfc3986-validator: 1018.58 MB
rpds-py: 1018.58 MB
sdkmanager: 169.51 MB
SecretStorage: 169.51 MB
selenium: 1018.58 MB
Send2Trash: 1018.58 MB
six: 169.51 MB
smart-open: 1018.58 MB
sniffio: 1018.58 MB
sortedcontainers: 1018.58 MB
soupsieve: 1018.58 MB
spacy: 1018.58 MB
spacy-legacy: 1018.58 MB
spacy-loggers: 1018.58 MB
WARNING: Package(s) not found: Spire.Doc @ file:///home/muthu/Downloads/Spire.Doc-12.1.0-py3-none-manylinux1_x86_64.whl#sha256=07235c8d63b315d0edbfeb23abd3a00176a1dbb874250e8e92ea1a3d2c90144c
Spire.Doc @ file:///home/muthu/Downloads/Spire.Doc-12.1.0-py3-none-manylinux1_x86_64.whl#sha256=07235c8d63b315d0edbfeb23abd3a00176a1dbb874250e8e92ea1a3d2c90144c: 0.00 MB
SQLAlchemy: 1018.58 MB
srsly: 1018.58 MB
stack-data: 1018.58 MB
systemd-python: 169.51 MB
terminado: 1018.58 MB
texttable: 1018.58 MB
thinc: 1018.58 MB
tinycss2: 1018.58 MB
tkhtmlview: 1018.58 MB
tkinter-tooltip: 1018.58 MB
tkVideo: 1018.58 MB
tomli: 1018.58 MB
tornado: 1018.58 MB
tqdm: 1018.58 MB
traitlets: 1018.58 MB
trio: 1018.58 MB
trio-websocket: 1018.58 MB
ttkbootstrap: 1018.58 MB
typer: 1018.58 MB
types-python-dateutil: 1018.58 MB
typing_extensions: 1018.58 MB
tzdata: 1018.58 MB
tzlocal: 1018.58 MB
ubuntu-drivers-common: 169.51 MB
ubuntu-pro-client: 169.51 MB
ufw: 169.51 MB
unattended-upgrades: 169.51 MB
unicodeit: 1018.58 MB
uri-template: 1018.58 MB
urllib3: 169.51 MB
usb-creator: 169.51 MB
vboxapi: 169.51 MB
virtualenv: 169.51 MB
wadllib: 169.51 MB
wasabi: 1018.58 MB
wcwidth: 1018.58 MB
weasel: 1018.58 MB
webcolors: 1018.58 MB
webdriver-manager: 1018.58 MB
webencodings: 1018.58 MB
websocket-client: 1018.58 MB
Werkzeug: 1018.58 MB
wsproto: 1018.58 MB
wxPython: 169.51 MB
xdg: 169.51 MB
xkit: 169.51 MB
XlsxWriter: 1018.58 MB
zipp: 169.51 MB

Total size of all packages: 212694.09 MB
© www.soinside.com 2019 - 2024. All rights reserved.