]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-134872: add ModuleNotFoundError suggestions (#142512)
authorFilipe Laíns <lains@riseup.net>
Thu, 19 Feb 2026 16:10:58 +0000 (16:10 +0000)
committerGitHub <noreply@github.com>
Thu, 19 Feb 2026 16:10:58 +0000 (16:10 +0000)
* gh-134872: Add traceback suggestions for ModuleNotFoundError

Signed-off-by: Filipe Laíns <lains@riseup.net>
* Add news

Signed-off-by: Filipe Laíns <lains@riseup.net>
---------

Signed-off-by: Filipe Laíns <lains@riseup.net>
Lib/traceback.py
Misc/NEWS.d/next/Library/2026-02-19-15-42-06.gh-issue-134872.sjYX1-.rst [new file with mode: 0644]

index 42453b4867ce994fd839bf876523af8d5be83bf4..b16cd8646e43f1592b8c01f24ec8ac955165eac1 100644 (file)
@@ -11,6 +11,7 @@ import codeop
 import keyword
 import tokenize
 import io
+import importlib.util
 import _colorize
 
 from contextlib import suppress
@@ -1128,6 +1129,10 @@ class TracebackException:
                 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)
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2026-02-19-15-42-06.gh-issue-134872.sjYX1-.rst b/Misc/NEWS.d/next/Library/2026-02-19-15-42-06.gh-issue-134872.sjYX1-.rst
new file mode 100644 (file)
index 0000000..4654dd0
--- /dev/null
@@ -0,0 +1 @@
+Add valid import name suggestions on :exc:`ModuleNotFoundError`.