Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import keyword
import tokenize
import io
import importlib.util
import _colorize

from contextlib import suppress
Expand Down Expand Up @@ -1128,6 +1129,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
self._str += (". Site initialization is disabled, did you forget to "
+ "add the site-packages directory to sys.path "
+ "or to enable your virtual environment?")
else:
suggestion = _compute_suggestion_error(exc_value, exc_traceback, module_name)
if suggestion:
self._str += f". Did you mean: '{suggestion}'?"
elif exc_type and issubclass(exc_type, AttributeError) and \
getattr(exc_value, "name", None) is not None:
wrong_name = getattr(exc_value, "name", None)
Expand Down Expand Up @@ -1717,6 +1722,18 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
d = [x for x in d if x[:1] != '_']
except Exception:
return None
elif isinstance(exc_value, ModuleNotFoundError):
try:
if parent_name := wrong_name.rpartition('.')[0]:
parent = importlib.util.find_spec(parent_name)
else:
parent = None
d = []
for finder in sys.meta_path:
if discover := getattr(finder, 'discover', None):
d += [spec.name for spec in discover(parent)]
except Exception:
return None
elif isinstance(exc_value, ImportError):
try:
mod = __import__(exc_value.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add valid import name suggestions on :exc:`ModuleNotFoundError`.
Loading