延迟导入Python模块的几种方法

发布时间 2023-10-23 11:16:33作者: 你说夕阳很美

延迟导入Python模块的几种方法 - 知乎 (zhihu.com)

# __init__.py

import importlib

__all__ = ['complicated']

def __getattr__(name):
  if name in __all__:
    return importlib.import_module("." + name, __name__)
  else:
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

def __dir__():
  return __all__