Loads a module and all its submodules from the given module path and returns them. If *any* module throws an exception while importing, that exception is thrown back. For example: >>> list(walk_modules_iter('scrapy.utils')) [<module 'scrapy.utils' from '...'>, ...] >>> gen =
(path: str)
| 91 | |
| 92 | |
| 93 | def walk_modules_iter(path: str) -> Iterable[ModuleType]: |
| 94 | """Loads a module and all its submodules from the given module path and |
| 95 | returns them. If *any* module throws an exception while importing, that |
| 96 | exception is thrown back. |
| 97 | |
| 98 | For example: |
| 99 | >>> list(walk_modules_iter('scrapy.utils')) |
| 100 | [<module 'scrapy.utils' from '...'>, ...] |
| 101 | >>> gen = walk_modules_iter('scrapy.utils.nonexistent') # error not raised until the generator is consumed |
| 102 | >>> list(gen) |
| 103 | Traceback (most recent call last): |
| 104 | ... |
| 105 | ModuleNotFoundError: No module named 'scrapy.utils.nonexistent' |
| 106 | """ |
| 107 | |
| 108 | mod = import_module(path) |
| 109 | yield mod |
| 110 | if hasattr(mod, "__path__"): |
| 111 | for _, subpath, ispkg in iter_modules(mod.__path__): |
| 112 | fullpath = path + "." + subpath |
| 113 | if ispkg: |
| 114 | yield from walk_modules_iter(fullpath) |
| 115 | else: |
| 116 | yield import_module(fullpath) |
| 117 | |
| 118 | |
| 119 | def walk_modules(path: str) -> list[ModuleType]: # pragma: no cover |
no outgoing calls