It looks simple and easy to understand.
To achieve lazy import without breaking backward compatibility,
I want to add one more rule: When package defines both of __getattr__ and __all__, automatic import of submodules are disabled (sorry, I don't have pointer to specification about this behavior).
For example, some modules depends on email.parser or email.feedparser.
But since email/__init__.py uses __all__, all submodules
are imported eagerly.
Changing __all__ will break backward compatibility.
With __getattr__, this can be lazy import:
import importlib
def __getattr__(name):
if name in __all__:
return importlib.import_module("." + name, __name__)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
Regards,