Finding a subpackage via importlib.machinery.ModuleSpec


pkgutil.walk_packages() imports all encountered packages, plus it has some drawbacks (

What if I just want to import a single subpackage among a package hierarchy ? I was expecting importlib to provide a function for searching a subpackage in a hierarchy of packages/modules, but I didn’t find one.

I just discovered importlib utilities around importlib.machinery.ModuleSpec, especially its submodule_search_locations attribute. Is is appropriate to use it for going down the hierarchy, listing packages/modules within each locations - with e.g. os.scandir() ?