fix use of importlib.util.find_spec

This commit is contained in:
David Lord 2023-06-09 09:34:42 -07:00
parent c8cf4694c6
commit bda295d37f
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 40 additions and 82 deletions

View file

@ -575,13 +575,20 @@ def get_root_path(import_name: str) -> str:
return os.path.dirname(os.path.abspath(mod.__file__))
# Next attempt: check the loader.
spec = importlib.util.find_spec(import_name)
loader = spec.loader if spec is not None else None
try:
spec = importlib.util.find_spec(import_name)
if spec is None:
raise ValueError
except (ImportError, ValueError):
loader = None
else:
loader = spec.loader
# Loader does not exist or we're referring to an unloaded main
# module or a main module without path (interactive sessions), go
# with the current working directory.
if loader is None or import_name == "__main__":
if loader is None:
return os.getcwd()
if hasattr(loader, "get_filename"):