Skip to content

Commit 47d2f68

Browse files
pythongh-139707: Better ModuleNotFoundError message for missing stdlib modules (pythonGH-140219)
1 parent b2f9fb9 commit 47d2f68

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Lib/test/test_traceback.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5034,7 +5034,8 @@ def test_no_site_package_flavour(self):
50345034

50355035
self.assertIn(
50365036
(b"Site initialization is disabled, did you forget to "
5037-
b"add the site-packages directory to sys.path?"), stderr
5037+
b"add the site-packages directory to sys.path "
5038+
b"or to enable your virtual environment?"), stderr
50385039
)
50395040

50405041
code = """
@@ -5046,9 +5047,20 @@ def test_no_site_package_flavour(self):
50465047

50475048
self.assertNotIn(
50485049
(b"Site initialization is disabled, did you forget to "
5049-
b"add the site-packages directory to sys.path?"), stderr
5050+
b"add the site-packages directory to sys.path "
5051+
b"or to enable your virtual environment?"), stderr
50505052
)
50515053

5054+
def test_missing_stdlib_package(self):
5055+
code = """
5056+
import sys
5057+
sys.stdlib_module_names |= {'spam'}
5058+
import spam
5059+
"""
5060+
_, _, stderr = assert_python_failure('-S', '-c', code)
5061+
5062+
self.assertIn(b"Standard library module 'spam' was not found", stderr)
5063+
50525064

50535065
class TestColorizedTraceback(unittest.TestCase):
50545066
maxDiff = None

Lib/traceback.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,11 +1107,14 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11071107
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
11081108
if suggestion:
11091109
self._str += f". Did you mean: '{suggestion}'?"
1110-
elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \
1111-
sys.flags.no_site and \
1112-
getattr(exc_value, "name", None) not in sys.stdlib_module_names:
1113-
self._str += (". Site initialization is disabled, did you forget to "
1114-
+ "add the site-packages directory to sys.path?")
1110+
elif exc_type and issubclass(exc_type, ModuleNotFoundError):
1111+
module_name = getattr(exc_value, "name", None)
1112+
if module_name in sys.stdlib_module_names:
1113+
self._str = f"Standard library module '{module_name}' was not found"
1114+
elif sys.flags.no_site:
1115+
self._str += (". Site initialization is disabled, did you forget to "
1116+
+ "add the site-packages directory to sys.path "
1117+
+ "or to enable your virtual environment?")
11151118
elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \
11161119
getattr(exc_value, "name", None) is not None:
11171120
wrong_name = getattr(exc_value, "name", None)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`ModuleNotFoundError` error message when a :term:`standard library`
2+
module is missing.

0 commit comments

Comments
 (0)