mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-139707: Better ModuleNotFoundError message for missing stdlib modules (GH-140219)
This commit is contained in:
parent
b2f9fb9db2
commit
47d2f68df2
3 changed files with 24 additions and 7 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue