11# Copyright (c) OpenMMLab. All rights reserved.
22import os .path as osp
33import subprocess
4- from typing import Any
54from importlib .metadata import PackageNotFoundError , distribution
6-
5+ from typing import Any
76
87
98def is_installed (package : str ) -> bool :
@@ -18,7 +17,7 @@ def is_installed(package: str) -> bool:
1817 spec = importlib .util .find_spec (package )
1918 if spec is not None and spec .origin is not None :
2019 return True
21-
20+
2221 # If not found as module, check if it's a distribution package
2322 try :
2423 distribution (package )
@@ -43,7 +42,6 @@ def get_installed_path(package: str) -> str:
4342 # inferred. For example, mmcv-full is the package name, but mmcv is module
4443 # name. If we want to get the installed path of mmcv-full, we should concat
4544 # the pkg.location and module name
46-
4745 # Try to get location from distribution package metadata
4846 location = None
4947 try :
@@ -52,7 +50,7 @@ def get_installed_path(package: str) -> str:
5250 location = str (locate_result .parent )
5351 except PackageNotFoundError :
5452 pass
55-
53+
5654 # If distribution package not found, try to find via importlib
5755 if location is None :
5856 spec = importlib .util .find_spec (package )
@@ -88,11 +86,12 @@ def package2module(package: str) -> str:
8886 # In importlib.metadata,
8987 # top-level modules are in dist.read_text('top_level.txt')
9088 top_level_text = dist .read_text ('top_level.txt' )
91- if top_level_text is None :
92- module_name = top_level_text .split ('\n ' )[0 ]
93- return module_name
94- else :
95- raise ValueError (f'can not infer the module name of { package } ' )
89+ if top_level_text is not None :
90+ lines = top_level_text .strip ().split ('\n ' )
91+ if lines :
92+ module_name = lines [0 ].strip ()
93+ return module_name
94+ raise ValueError (f'can not infer the module name of { package } ' )
9695
9796
9897def call_command (cmd : list ) -> None :
0 commit comments