Skip to content

Commit aafa142

Browse files
committed
[Fix] Fix get_installed_path for python3.12 and editable installed package
1 parent 508cb32 commit aafa142

1 file changed

Lines changed: 38 additions & 41 deletions

File tree

mmengine/utils/package_utils.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os.path as osp
33
import subprocess
44
from importlib.metadata import PackageNotFoundError, distribution
5-
from typing import Any
65

76

87
def is_installed(package: str) -> bool:
@@ -32,64 +31,62 @@ def get_installed_path(package: str) -> str:
3231
Args:
3332
package (str): Name of package.
3433
34+
Returns:
35+
str: The installed path of the package.
36+
3537
Example:
3638
>>> get_installed_path('mmcls')
3739
>>> '.../lib/python3.7/site-packages/mmcls'
3840
"""
3941
import importlib.util
4042

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`')
7562

7663

7764
def package2module(package: str) -> str:
7865
"""Infer module name from package.
7966
8067
Args:
8168
package (str): Package to infer module name.
69+
70+
Returns:
71+
str: The inferred module name.
8272
"""
83-
dist = distribution(package)
73+
import importlib.util
8474

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)
8784
top_level_text = dist.read_text('top_level.txt')
8885
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()]
9088
if lines:
91-
module_name = lines[0].strip()
92-
return module_name
89+
return lines[0]
9390
raise ValueError(f'can not infer the module name of {package}')
9491

9592

0 commit comments

Comments
 (0)