44from collections .abc import Iterable
55from importlib .metadata import Distribution
66
7- from .._compat import loadedPackages
8- from .._utils import get_files_in_distribution , get_root
7+ from .._uninstall import _uninstall
98
109
11- def uninstall (packages : str | Iterable [str ], * , ignore_missing : bool = False ) -> None :
10+ def uninstall (packages : str | Iterable [str ]) -> None :
1211 """Uninstall the given packages.
1312
1413 This function only supports uninstalling packages that are installed
@@ -23,9 +22,6 @@ def uninstall(packages: str | Iterable[str], *, ignore_missing: bool = False) ->
2322 ----------
2423 packages
2524 Packages to uninstall.
26-
27- _ignore_missing
28- If ``True``, suppress warnings when a package is not installed.
2925 """
3026
3127 if isinstance (packages , str ):
@@ -37,61 +33,11 @@ def uninstall(packages: str | Iterable[str], *, ignore_missing: bool = False) ->
3733 dist = importlib .metadata .distribution (package )
3834 distributions .append (dist )
3935 except importlib .metadata .PackageNotFoundError :
40- if not ignore_missing : # TODO: Can we utilize log level here?
41- warnings .warn (
42- f"WARNING: Skipping '{ package } ' as it is not installed." ,
43- stacklevel = 1 ,
44- )
45-
46- for dist in distributions :
47- # Note: this value needs to be retrieved before removing files, as
48- # dist.name uses metadata file to get the name
49- name = dist .name
50-
51- root = get_root (dist )
52- files = get_files_in_distribution (dist )
53- directories = set ()
54-
55- for file in files :
56- if not file .is_file ():
57- if not file .is_relative_to (root ):
58- # This file is not in the site-packages directory. Probably one of:
59- # - data_files
60- # - scripts
61- # - entry_points
62- # Since we don't support these, we can ignore them (except for data_files (TODO))
63- continue
64-
65- warnings .warn (
66- f"WARNING: A file '{ file } ' listed in the metadata of '{ dist .name } ' does not exist." ,
67- stacklevel = 1 ,
68- )
69-
70- continue
71-
72- file .unlink ()
73-
74- if file .parent != root :
75- directories .add (file .parent )
76-
77- # Remove directories in reverse hierarchical order
78- for directory in sorted (directories , key = lambda x : len (x .parts ), reverse = True ):
79- try :
80- directory .rmdir ()
81- except OSError :
82- warnings .warn (
83- f"WARNING: A directory '{ directory } ' is not empty after uninstallation of '{ name } '. "
84- "This might cause problems when installing a new version of the package. " ,
85- stacklevel = 1 ,
86- )
87-
88- if hasattr (loadedPackages , name ):
89- delattr (loadedPackages , name )
90- else :
91- # This should not happen, but just in case
9236 warnings .warn (
93- f"WARNING: a package ' { name } ' was not found in loadedPackages ." ,
37+ f"WARNING: Skipping ' { package } ' as it is not installed ." ,
9438 stacklevel = 1 ,
9539 )
9640
41+ _uninstall (distributions )
42+
9743 importlib .invalidate_caches ()
0 commit comments