| 1 |
"""Getting specific information for ModuleNotFoundError""" |
| 2 |
|
| 3 |
import re |
| 4 |
import sys |
| 5 |
|
| 6 |
from .. import debug_helper |
| 7 |
from ..ft_gettext import current_lang |
| 8 |
from ..message_parser import get_parser |
| 9 |
from ..tb_data import TracebackData |
| 10 |
from ..typing_info import CauseInfo |
| 11 |
from ..utils import get_similar_words, list_to_string |
| 12 |
from . import stdlib_modules |
| 13 |
|
| 14 |
parser = get_parser(ModuleNotFoundError) |
| 15 |
_ = current_lang.translate |
| 16 |
|
| 17 |
|
| 18 |
@parser._add |
| 19 |
def is_not_a_package(message: str, _tb_data: TracebackData) -> CauseInfo: |
| 20 |
|
| 21 |
pattern = re.compile(r"No module named\s*'(.*)'; '(.*)' is not a package") |
| 22 |
match = re.search(pattern, message) |
| 23 |
if not match: |
| 24 |
return {} |
| 25 |
|
| 26 |
hint = "" |
| 27 |
dotted_path = match[1] |
| 28 |
name = match[2] |
| 29 |
rest = dotted_path.replace(name + ".", "") |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
try: |
| 34 |
module = __import__(name) |
| 35 |
except ImportError as e: |
| 36 |
cause = _( |
| 37 |
"No additional information available since `{name}` cannot be imported.\n" |
| 38 |
).format(name=name) |
| 39 |
debug_helper.log("Problem in is_not_a_package()") |
| 40 |
debug_helper.log(str(e)) |
| 41 |
debug_helper.log(cause) |
| 42 |
return {"cause": cause} |
| 43 |
|
| 44 |
attributes = dir(module) |
| 45 |
|
| 46 |
if rest in attributes: |
| 47 |
hint = _("Did you mean `from {name} import {rest}`?\n").format( |
| 48 |
name=name, rest=rest |
| 49 |
) |
| 50 |
cause = _( |
| 51 |
"`{rest}` is not a separate module but an object that is part of `{name}`.\n" |
| 52 |
).format(name=name, rest=rest) |
| 53 |
return {"cause": cause, "suggest": hint} |
| 54 |
|
| 55 |
similar = get_similar_words(rest, attributes) |
| 56 |
if similar: |
| 57 |
for attr in similar: |
| 58 |
obj = getattr(module, attr) |
| 59 |
if isinstance(obj, type(module)): |
| 60 |
hint = _("Did you mean `import {name}.{attr}`?\n").format( |
| 61 |
name=name, attr=attr |
| 62 |
) |
| 63 |
cause = _( |
| 64 |
"Perhaps you meant `import {name}.{attr}`.\n" |
| 65 |
"`{attr}` is a name similar to `{rest}` and is a module that\n" |
| 66 |
"can be imported from `{name}`.\n" |
| 67 |
).format(name=name, attr=attr, rest=rest) |
| 68 |
break |
| 69 |
else: |
| 70 |
attr = similar[0] |
| 71 |
hint = _("Did you mean `from {name} import {attr}`?\n").format( |
| 72 |
name=name, attr=attr |
| 73 |
) |
| 74 |
cause = _( |
| 75 |
"Perhaps you meant `from {name} import {attr}`.\n" |
| 76 |
"`{attr}` is a name similar to `{rest}` and is an object that\n" |
| 77 |
"can be imported from `{name}`.\n" |
| 78 |
).format(name=name, attr=attr, rest=rest) |
| 79 |
|
| 80 |
if len(similar) > 1: |
| 81 |
cause += _( |
| 82 |
"Other objects with similar names that are part of\n" |
| 83 |
" `{name}` include `{others}`.\n" |
| 84 |
).format(name=name, others=list_to_string(similar[1:])) |
| 85 |
return {"cause": cause, "suggest": hint} |
| 86 |
else: |
| 87 |
cause = _("`{rest}` cannot be imported from `{name}`.\n").format( |
| 88 |
rest=rest, name=name |
| 89 |
) |
| 90 |
|
| 91 |
return {"cause": cause, "suggest": hint} if hint else {"cause": cause} |
| 92 |
|
| 93 |
|
| 94 |
def curses_no_found() -> CauseInfo: |
| 95 |
if sys.platform.startswith("win"): |
| 96 |
hint = _("The curses module is rarely installed with Python on Windows.\n") |
| 97 |
else: |
| 98 |
hint = _("The curses module is often not installed with Python.\n") |
| 99 |
cause = _("You have tried to import the curses module.\n") |
| 100 |
return {"cause": cause + hint, "suggest": hint} |
| 101 |
|
| 102 |
|
| 103 |
@parser._add |
| 104 |
def no_module_named(message: str, _tb_data: TracebackData) -> CauseInfo: |
| 105 |
pattern = re.compile(r"No module named '(.*)'$") |
| 106 |
match = re.search(pattern, message) |
| 107 |
if not match: |
| 108 |
return {} |
| 109 |
|
| 110 |
name = match[1] |
| 111 |
if name == "_curses": |
| 112 |
return curses_no_found() |
| 113 |
|
| 114 |
if name in stdlib_modules.names and not stdlib_modules.module_exists(name): |
| 115 |
return { |
| 116 |
"cause": _( |
| 117 |
"No module named `{name}` can be imported.\n\n" |
| 118 |
"Note that there is a module named `{name}` that is part of the\n" |
| 119 |
"Python standard library. However, it might not be available\n" |
| 120 |
"for your operating system, or it may to be installed separately.\n" |
| 121 |
).format(name=name) |
| 122 |
} |
| 123 |
|
| 124 |
similar = get_similar_words(name, stdlib_modules.names) |
| 125 |
similar = [ |
| 126 |
mod_name for mod_name in similar if stdlib_modules.module_exists(mod_name) |
| 127 |
] |
| 128 |
|
| 129 |
cause = _( |
| 130 |
"No module named `{name}` can be imported.\n" |
| 131 |
"Perhaps you need to install it.\n" |
| 132 |
).format(name=name) |
| 133 |
if not similar: |
| 134 |
return {"cause": cause} |
| 135 |
|
| 136 |
hint = _("Did you mean `{name}`?\n").format(name=similar[0]) |
| 137 |
if len(similar) > 1: |
| 138 |
cause += _( |
| 139 |
"The following existing modules have names that are similar \n" |
| 140 |
"to the module you tried to import: `{names}`\n" |
| 141 |
).format(names=", ".join(similar)) |
| 142 |
else: |
| 143 |
cause += _("`{name}` is an existing module that has a similar name.\n").format( |
| 144 |
name=similar[0] |
| 145 |
) |
| 146 |
return {"cause": cause, "suggest": hint} |