New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An os.walk inspired replacement for pkgutil.walk_packages #61264
Comments
I recently had occasion to use pkgutil.walk_packages, and my immediate thought was that it would have been a lot easier for me to use if it worked more like os.walk with topdown=True, producing tuples of (pkg, subpackages, modules) "pkg" would be the package object at the current level (None for the top level) "packages" would be a dictionary mapping fully qualified module names to loader objects for the subpackages (i.e. subdirectories) "modules" would be a dictionary mapping fully qualified module names to loader objects for every submodule that wasn't a subpackage As with editing the "subdirs" list with os.walk, editing the "packages" dictionary with this new API would keep the iterator from loading that subpackage and avoid recursing into it (this is the part I wanted in my current use case). (This may even be PEP material, guiding some additions to the importer/finder API) |
Oops, forgot the proposed call signature: def walk_path(path=None, *, pkg=None):
"""Walk a package hierarchy, starting with the given path
|
Regarding the PEP comment - the piece that would be missing is the "iter_modules" functionality. Currently pkgutil provides the support for standard filesystem imports and zipimports directly - the generic function based extension mechanism is undocumented. |
I threw together a function that implements this. The only variation from the proposed signature was adding the onerror argument supported by the other similar functions in the module. |
Could somebody review the attached patch please. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: