https://github.com/python/cpython/commit/47d2f68df215b5bcbc76a50d3f4e0b68791e3e79
commit: 47d2f68df215b5bcbc76a50d3f4e0b68791e3e79
branch: main
author: Stan Ulbrych <[email protected]>
committer: encukou <[email protected]>
date: 2025-10-21T09:12:04+02:00
summary:

gh-139707: Better `ModuleNotFoundError` message for missing stdlib modules 
(GH-140219)

files:
A Misc/NEWS.d/next/Library/2025-10-16-16-10-11.gh-issue-139707.zR6Qtn.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index bd3ecfd9a3863d..bf57867a8715c0 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -5034,7 +5034,8 @@ def test_no_site_package_flavour(self):
 
         self.assertIn(
             (b"Site initialization is disabled, did you forget to "
-                b"add the site-packages directory to sys.path?"), stderr
+             b"add the site-packages directory to sys.path "
+             b"or to enable your virtual environment?"), stderr
         )
 
         code = """
@@ -5046,9 +5047,20 @@ def test_no_site_package_flavour(self):
 
         self.assertNotIn(
             (b"Site initialization is disabled, did you forget to "
-                b"add the site-packages directory to sys.path?"), stderr
+             b"add the site-packages directory to sys.path "
+             b"or to enable your virtual environment?"), stderr
         )
 
+    def test_missing_stdlib_package(self):
+        code = """
+            import sys
+            sys.stdlib_module_names |= {'spam'}
+            import spam
+        """
+        _, _, stderr = assert_python_failure('-S', '-c', code)
+
+        self.assertIn(b"Standard library module 'spam' was not found", stderr)
+
 
 class TestColorizedTraceback(unittest.TestCase):
     maxDiff = None
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 692d44837936ee..9b4b8c7d566fe8 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1107,11 +1107,14 @@ def __init__(self, exc_type, exc_value, exc_traceback, 
*, limit=None,
             suggestion = _compute_suggestion_error(exc_value, exc_traceback, 
wrong_name)
             if suggestion:
                 self._str += f". Did you mean: '{suggestion}'?"
-        elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \
-                sys.flags.no_site and \
-                getattr(exc_value, "name", None) not in 
sys.stdlib_module_names:
-            self._str += (". Site initialization is disabled, did you forget 
to "
-                + "add the site-packages directory to sys.path?")
+        elif exc_type and issubclass(exc_type, ModuleNotFoundError):
+            module_name = getattr(exc_value, "name", None)
+            if module_name in sys.stdlib_module_names:
+                self._str = f"Standard library module '{module_name}' was not 
found"
+            elif sys.flags.no_site:
+                self._str += (". Site initialization is disabled, did you 
forget to "
+                    + "add the site-packages directory to sys.path "
+                    + "or to enable your virtual environment?")
         elif exc_type and issubclass(exc_type, (NameError, AttributeError)) 
and \
                 getattr(exc_value, "name", None) is not None:
             wrong_name = getattr(exc_value, "name", None)
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-16-16-10-11.gh-issue-139707.zR6Qtn.rst 
b/Misc/NEWS.d/next/Library/2025-10-16-16-10-11.gh-issue-139707.zR6Qtn.rst
new file mode 100644
index 00000000000000..c5460aae8b3638
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-16-16-10-11.gh-issue-139707.zR6Qtn.rst
@@ -0,0 +1,2 @@
+Improve :exc:`ModuleNotFoundError` error message when a :term:`standard 
library`
+module is missing.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to