From 11f827c58b276dff946dccf4167d8e11159eeba5 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:33:02 +0900 Subject: [PATCH] use pip freeze --all to get packages --- modules/sysinfo.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 614334661..65d4e3c98 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -4,7 +4,6 @@ import sys import platform import hashlib -import pkg_resources import re import launch @@ -88,6 +87,19 @@ def get_ram_info(): return str(e) +def get_packages(): + try: + import subprocess + return subprocess.check_output([sys.executable, '-m', 'pip', 'freeze', '--all']).decode("utf8").splitlines() + except Exception as pip_error: + try: + import importlib.metadata + packages = importlib.metadata.distributions() + return sorted([f"{package.metadata['Name']}=={package.version}" for package in packages]) + except Exception as e2: + return {'error pip': pip_error, 'error importlib': str(e2)} + + def get_dict(): res = { "Platform": platform.platform(), @@ -108,7 +120,7 @@ def get_dict(): "Environment": get_environment(), "Config": get_config(), "Startup": timer.startup_record, - "Packages": sorted([f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set]), + "Packages": get_packages(), } return res