|
2 | 2 | import os.path as osp |
3 | 3 | import subprocess |
4 | 4 | from importlib.metadata import PackageNotFoundError, distribution |
5 | | -from typing import Any |
6 | 5 |
|
7 | 6 |
|
8 | 7 | def is_installed(package: str) -> bool: |
@@ -32,64 +31,62 @@ def get_installed_path(package: str) -> str: |
32 | 31 | Args: |
33 | 32 | package (str): Name of package. |
34 | 33 |
|
| 34 | + Returns: |
| 35 | + str: The installed path of the package. |
| 36 | +
|
35 | 37 | Example: |
36 | 38 | >>> get_installed_path('mmcls') |
37 | 39 | >>> '.../lib/python3.7/site-packages/mmcls' |
38 | 40 | """ |
39 | 41 | import importlib.util |
40 | 42 |
|
41 | | - # if the package name is not the same as module name, module name should be |
42 | | - # inferred. For example, mmcv-full is the package name, but mmcv is module |
43 | | - # name. If we want to get the installed path of mmcv-full, we should concat |
44 | | - # the pkg.location and module name |
45 | | - # Try to get location from distribution package metadata |
46 | | - location = None |
47 | | - try: |
48 | | - dist = distribution(package) |
49 | | - locate_result: Any = dist.locate_file('') |
50 | | - location = str(locate_result.parent) |
51 | | - except PackageNotFoundError: |
52 | | - pass |
53 | | - |
54 | | - # If distribution package not found, try to find via importlib |
55 | | - if location is None: |
56 | | - spec = importlib.util.find_spec(package) |
57 | | - if spec is not None: |
58 | | - if spec.origin is not None: |
59 | | - return osp.dirname(spec.origin) |
60 | | - else: |
61 | | - # `get_installed_path` cannot get the installed path of |
62 | | - # namespace packages |
63 | | - raise RuntimeError( |
64 | | - f'{package} is a namespace package, which is invalid ' |
65 | | - 'for `get_install_path`') |
66 | | - else: |
67 | | - raise PackageNotFoundError(f'Package {package} is not installed') |
68 | | - |
69 | | - # Check if package directory exists in the location |
70 | | - possible_path = osp.join(location, package) |
71 | | - if osp.exists(possible_path): |
72 | | - return possible_path |
73 | | - else: |
74 | | - return osp.join(location, package2module(package)) |
| 43 | + # Resolve the location through the import machinery rather than the |
| 44 | + # distribution `location`. `find_spec` correctly handles regular, |
| 45 | + # editable (installs that expose the module via a `.pth`/import hook while |
| 46 | + # the files live outside site-packages), and `PYTHONPATH` installs, whereas |
| 47 | + # distribution metadata reports the site-packages directory even when the |
| 48 | + # module is not physically there. |
| 49 | + module_name = package2module(package) |
| 50 | + spec = importlib.util.find_spec(module_name) |
| 51 | + if spec is None: |
| 52 | + raise PackageNotFoundError(f'Package {package} is not installed') |
| 53 | + |
| 54 | + if spec.origin is not None: |
| 55 | + return osp.dirname(spec.origin) |
| 56 | + if spec.submodule_search_locations: |
| 57 | + return spec.submodule_search_locations[0] |
| 58 | + # A namespace package has neither an origin nor a single concrete location. |
| 59 | + raise RuntimeError( |
| 60 | + f'{package} is a namespace package, which is invalid for ' |
| 61 | + '`get_installed_path`') |
75 | 62 |
|
76 | 63 |
|
77 | 64 | def package2module(package: str) -> str: |
78 | 65 | """Infer module name from package. |
79 | 66 |
|
80 | 67 | Args: |
81 | 68 | package (str): Package to infer module name. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + str: The inferred module name. |
82 | 72 | """ |
83 | | - dist = distribution(package) |
| 73 | + import importlib.util |
84 | 74 |
|
85 | | - # In importlib.metadata, |
86 | | - # top-level modules are in dist.read_text('top_level.txt') |
| 75 | + # The importable module name usually matches the package name. Probing the |
| 76 | + # import machinery first also covers editable installs, whose |
| 77 | + # `top_level.txt` may be absent even though the module is importable. |
| 78 | + if importlib.util.find_spec(package) is not None: |
| 79 | + return package |
| 80 | + |
| 81 | + # Distribution name differs from the module name (e.g. `mmcv-full` -> |
| 82 | + # `mmcv`); recover the top-level module from distribution metadata. |
| 83 | + dist = distribution(package) |
87 | 84 | top_level_text = dist.read_text('top_level.txt') |
88 | 85 | if top_level_text is not None: |
89 | | - lines = top_level_text.strip().split('\n') |
| 86 | + lines = [line.strip() for line in top_level_text.splitlines() |
| 87 | + if line.strip()] |
90 | 88 | if lines: |
91 | | - module_name = lines[0].strip() |
92 | | - return module_name |
| 89 | + return lines[0] |
93 | 90 | raise ValueError(f'can not infer the module name of {package}') |
94 | 91 |
|
95 | 92 |
|
|
0 commit comments