diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 5dc11253e0d5c8..af7a75bd3cda8d 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -5189,6 +5189,15 @@ def test_no_site_package_flavour(self): b"or to enable your virtual environment?"), stderr ) + code = """import abs""" + _, _, stderr = assert_python_failure('-S', '-c', code) + self.assertIn(b"Did you mean: 'abc'?", stderr) + self.assertIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path " + b"or to enable your virtual environment?"), stderr + ) + def test_missing_stdlib_module(self): code = """ import sys @@ -5266,9 +5275,17 @@ def test_incompatible_extension_modules_hint(self): sys.path.insert(0, {tmp!r}) import foo ''' - _, _, stderr = assert_python_failure('-c', code, __cwd=tmp) + _, _, stderr1 = assert_python_failure('-c', code, __cwd=tmp) + # if used "-S", the message should contain two suggestions + _, _, stderr2 = assert_python_failure('-S', '-c', code, __cwd=tmp) hint = f'Although a module with this name was found for a different Python version ({incompatible_module}).' - self.assertIn(hint, stderr.decode()) + self.assertIn(hint, stderr1.decode()) + self.assertIn(hint, stderr2.decode()) + self.assertIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path " + b"or to enable your virtual environment?"), stderr2 + ) class TestColorizedTraceback(unittest.TestCase): diff --git a/Lib/traceback.py b/Lib/traceback.py index 1f9f151ebf5d39..5c13540551c8e7 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1125,16 +1125,14 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, self._str += f". Did you mean: '{suggestion}' ({suggestion!a})?" elif exc_type and issubclass(exc_type, ModuleNotFoundError): module_name = getattr(exc_value, "name", None) + analyse_sys_no_site = True if module_name in sys.stdlib_module_names: message = _MISSING_STDLIB_MODULE_MESSAGES.get( module_name, f"Standard library module {module_name!r} was not found" ) self._str = message - 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?") + analyse_sys_no_site = False elif abi_tag := _find_incompatible_extension_module(module_name): self._str += ( ". Although a module with this name was found for a " @@ -1144,6 +1142,12 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, suggestion = _compute_suggestion_error(exc_value, exc_traceback, module_name) if suggestion: self._str += f". Did you mean: '{suggestion}'?" + if analyse_sys_no_site and sys.flags.no_site: + if not self._str.endswith((".", "?")): + self._str += "." + 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, AttributeError) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None)