Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions 16 Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions 13 Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`ModuleNotFoundError` error message when a :term:`standard library`
module is missing.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.