def list_submodules(self,package_path): """递归地列出给定包路径下的所有子模块""" all_submodules = [] # 遍历包路径下的所有文件/目录 for importer, modname, ispkg in pkgutil.iter_modules([package_path]): full_modname = modname module_path = os.path.join(package_path, modname) # 如果是子包,则递归调用list_submodules if ispkg: # 构建子包的完整路径 subpackage_path = os.path.join(module_path, '__init__.py') # 递归查找子包下的子模块 submodules = self.list_submodules(subpackage_path) # 添加子模块名称到结果列表,同时保留子包的路径 all_submodules.extend([full_modname + '.' + submodule for submodule in submodules]) else: # 添加非包模块的完整名称 all_submodules.append(full_modname) return all_submodules
标签:modname,full,submodules,python,package,模块,某个,path From: https://www.cnblogs.com/shunguo/p/18064743