MCPcopy
hub / github.com/scrapy/scrapy / walk_modules_iter

Function walk_modules_iter

scrapy/utils/misc.py:93–116  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

91
92
93def 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
119def walk_modules(path: str) -> list[ModuleType]: # pragma: no cover

Callers 5

_load_all_spidersMethod · 0.90
_iter_command_classesFunction · 0.90
test_walk_modulesMethod · 0.90
test_walk_modules_eggMethod · 0.90
walk_modulesFunction · 0.85

Calls

no outgoing calls

Tested by 2

test_walk_modulesMethod · 0.72
test_walk_modules_eggMethod · 0.72