Determine Which Modules Are In A Package – Python

Below is a method to enumerate Python modules located in a directory:
{{{ lang=python
import pkgutil
def get_pkg_modules(pkg_path, recurse=False, add_prefix=”):
”’
Get modules of a package located at pkg_path
@param str pkg_path Path of package to inspect
@param bool recurse Recursive inspection?
@param str add_prefix Add this prefix to results
@return list List of modules located in package
”’
cmd = pkgutil.walk_packages if recurse else pkgutil.iter_modules
return list(
set([name for _, name, _ in cmd([pkg_path], add_prefix)])
)
}}}


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *