Skip to content

Commit

Permalink
scripts/uninstall_module: fix package discovery
Browse files Browse the repository at this point in the history
The `uninstall_module.py` script is a wrapper for the `pip uninstall`
command that enables support for specifying installation prefix
(i.e., `--prefix`). When this functionality is used, we intentionally
set `sys.path` to include only search paths for the specified prefix
to avoid unintentional uninstallation of packages in system paths.

Since `importlib_metadata` version 8.1.0, the `Distribution.from_name()`
method has been modified [1] to perform additional pre-processing of
Distribution objects [2] that requires loading distribution metadata
and results in the following error:

  File "/usr/local/lib/python3.12/site-packages/importlib_metadata/__init__.py", line 422, in <lambda>
    buckets = bucket(dists, lambda dist: bool(dist.metadata))
                                              ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/importlib_metadata/__init__.py", line 454, in metadata
    from . import _adapters
  File "/usr/local/lib/python3.12/site-packages/importlib_metadata/_adapters.py", line 3, in <module>
    import email.message
  File "/usr/lib64/python3.12/email/message.py", line 11, in <module>
    import quopri
  ModuleNotFoundError: No module named 'quopri'

This error occurs because we have excluded system paths from the list
of search paths (`sys.path`).

However, this pre-processing is not required for our use case, as we
only use the discovery mechanism of importlib_metadata to resolve the
metadata directory path of the module being uninstalled.

To fix this problem, this patch updates `uninstall_module` to avoid the
`from_name()` method and use `discover(name=package_name)` directly.

[1] python/importlib_metadata@a65c29a
[2] https://github.com/python/importlib_metadata/blob/a65c29ad/importlib_metadata/__init__.py#L391

Fixes: #2468

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git authored and avagin committed Aug 18, 2024
1 parent 5a74eee commit 4ca4a09
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions scripts/uninstall_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def uninstall_module(package_name: str, prefix=None):
if prefix:
add_site_dir(prefix)
try:
dist_info_path = str(importlib_metadata.distribution(package_name)._path)
except importlib_metadata.PackageNotFoundError:
distribution = next(importlib_metadata.Distribution.discover(name=package_name))
dist_info_path = str(distribution._path)
except StopIteration:
print(f"Skipping {package_name} as it is not installed.")
sys.exit(0)

Expand Down

0 comments on commit 4ca4a09

Please sign in to comment.