Message252975
My use case is for Readline auto-completion, to list submodules of a package given by the user, without importing anything unnecessary. The original code is compatible with Python 2, but I am also writing a patch for 3.6 that wouldn't need that. The original implementation is like this pseudocode:
def list_submodules(package):
# Ensure it is a package before loading or importing it
for name in parent_packages:
loader = pkgutil.find_loader(name)
assert loader.is_package()
# Could call importlib.import_module(), but this seemed easier because I already have the loader:
package = loader.load_module(name)
# The only reason I want to load the module:
search_path = package.__path__
return pkgutil.iter_modules(search_path)
Thanks for your feedback Brett. I have changed over to importlib.import_module(), and will accept that this is just a quirk of the low level import stuff. |
|
Date |
User |
Action |
Args |
2015-10-14 05:59:02 | martin.panter | set | recipients:
+ martin.panter, brett.cannon, ncoghlan, Arfrever, r.david.murray, eric.snow, Pathangi Jatinshravan |
2015-10-14 05:59:02 | martin.panter | set | messageid: <1444802342.28.0.583121347024.issue25372@psf.upfronthosting.co.za> |
2015-10-14 05:59:02 | martin.panter | link | issue25372 messages |
2015-10-14 05:59:01 | martin.panter | create | |
|