Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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)
Expand Down
Loading