Skip to content

Commit 65ca577

Browse files
committed
Refactor package information retrieval: replace pip list command with importlib.metadata
1 parent 6808edc commit 65ca577

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

datalab/widgets/instconfviewer.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os
1111
import platform
1212
import sys
13-
from subprocess import PIPE, Popen
13+
from importlib.metadata import distributions
1414

1515
from guidata.configtools import get_icon
1616
from guidata.qthelpers import exec_dialog
@@ -32,12 +32,24 @@ def decode_fs_string(string: bytes) -> str:
3232
return string.decode(charset)
3333

3434

35-
def get_pip_list() -> str:
36-
"""Get pip list result"""
37-
command = " ".join([sys.executable, "-m", "pip list"])
38-
with Popen(command, shell=True, stdout=PIPE, stderr=PIPE) as proc:
39-
output = proc.stdout.read()
40-
return decode_fs_string(output)
35+
def get_installed_package_info() -> str:
36+
"""Get the list of installed packages with their versions"""
37+
packages = [(dist.metadata["Name"], dist.version) for dist in distributions()]
38+
39+
# Sort alphabetically by package name
40+
packages.sort(key=lambda x: x[0].lower())
41+
42+
# Determine column widths
43+
name_width = max(len(name) for name, _ in packages)
44+
version_width = max(len(version) for _, version in packages)
45+
46+
header = f"{'Package':{name_width}} {'Version':{version_width}}"
47+
separator = f"{'-' * name_width} {'-' * version_width}"
48+
result_lines = [header, separator]
49+
for name, version in packages:
50+
result_lines.append(f"{name:{name_width}} {version:{version_width}}")
51+
52+
return os.linesep.join(result_lines)
4153

4254

4355
def get_install_info() -> str:
@@ -49,8 +61,8 @@ def get_install_info() -> str:
4961
if IS_FROZEN:
5062
# Stand-alone version
5163
more_info = "This is the Stand-alone version of DataLab."
52-
else:
53-
more_info = get_pip_list()
64+
more_info += os.linesep * 2
65+
more_info = get_installed_package_info()
5466
info = os.linesep.join(
5567
[
5668
f"DataLab v{datalab.__version__}",

0 commit comments

Comments
 (0)