Skip to content

Commit bc39544

Browse files
MGAMZHAOCHENYE
authored andcommitted
1. Accepting Google's guideline.
2. Only keep `locate_file` branch. 3. keep the logic of `distribution package` and `import package` independent.
1 parent 35601b1 commit bc39544

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

mmengine/utils/package_utils.py

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,19 @@ def is_installed(package: str) -> bool:
1212
Args:
1313
package (str): Name of package to be checked.
1414
"""
15-
# Use importlib.metadata instead of deprecated pkg_resources
16-
# importlib.metadata is available in Python 3.8+
17-
# For Python 3.7, importlib_metadata backport can be used
1815
import importlib.util
1916

17+
# First check if it's an importable module
18+
spec = importlib.util.find_spec(package)
19+
if spec is not None and spec.origin is not None:
20+
return True
21+
22+
# If not found as module, check if it's a distribution package
2023
try:
2124
distribution(package)
2225
return True
2326
except PackageNotFoundError:
24-
# If distribution not found, check if module can be imported
25-
spec = importlib.util.find_spec(package)
26-
if spec is None:
27-
return False
28-
elif spec.origin is not None:
29-
return True
30-
else:
31-
return False
27+
return False
3228

3329

3430
def get_installed_path(package: str) -> str:
@@ -47,29 +43,18 @@ def get_installed_path(package: str) -> str:
4743
# inferred. For example, mmcv-full is the package name, but mmcv is module
4844
# name. If we want to get the installed path of mmcv-full, we should concat
4945
# the pkg.location and module name
46+
47+
# Try to get location from distribution package metadata
48+
location = None
5049
try:
5150
dist = distribution(package)
52-
# In importlib.metadata, we use dist.locate_file() or files
53-
if hasattr(dist, 'locate_file'):
54-
# Python 3.9+
55-
# locate_file returns PathLike, need to access parent
56-
locate_result: Any = dist.locate_file('')
57-
location = str(locate_result.parent)
58-
elif hasattr(dist, '_path'):
59-
# Python 3.8 - _path is a pathlib.Path object
60-
# We know _path exists because we checked with hasattr
61-
dist_any: Any = dist
62-
location = str(dist_any._path.parent) # type: ignore[attr-defined]
63-
else:
64-
# Fallback: try to find via importlib
65-
spec = importlib.util.find_spec(package)
66-
if spec is not None and spec.origin is not None:
67-
return osp.dirname(spec.origin)
68-
raise RuntimeError(
69-
f'Cannot determine installation path for {package}')
70-
except PackageNotFoundError as e:
71-
# if the package is not installed, package path set in PYTHONPATH
72-
# can be detected by `find_spec`
51+
locate_result: Any = dist.locate_file('')
52+
location = str(locate_result.parent)
53+
except PackageNotFoundError:
54+
pass
55+
56+
# If distribution package not found, try to find via importlib
57+
if location is None:
7358
spec = importlib.util.find_spec(package)
7459
if spec is not None:
7560
if spec.origin is not None:
@@ -81,8 +66,10 @@ def get_installed_path(package: str) -> str:
8166
f'{package} is a namespace package, which is invalid '
8267
'for `get_install_path`')
8368
else:
84-
raise e
69+
raise PackageNotFoundError(
70+
f'Package {package} is not installed')
8571

72+
# Check if package directory exists in the location
8673
possible_path = osp.join(location, package)
8774
if osp.exists(possible_path):
8875
return possible_path
@@ -101,7 +88,7 @@ def package2module(package: str) -> str:
10188
# In importlib.metadata,
10289
# top-level modules are in dist.read_text('top_level.txt')
10390
top_level_text = dist.read_text('top_level.txt')
104-
if top_level_text:
91+
if top_level_text is None:
10592
module_name = top_level_text.split('\n')[0]
10693
return module_name
10794
else:

0 commit comments

Comments
 (0)