def is_package(module_name):
"""
Check if a Python module is really a module or is a package containing
other modules.
:param module_name: Module name to check.
:return: True if module is a package else otherwise.
"""
# This way determines if module is a package without importing the module.
try:
loader = pkgutil.find_loader(module_name)
except Exception:
# When it fails to find a module loader then it points probably to a class
# or function and module is not a package. Just return False.
return False
else:
if loader:
# A package must have a __path__ attribute.
return loader.is_package(module_name)
else:
# In case of None - modules is probably not a package.
return False
评论列表
文章目录