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 = """
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
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)