<div dir="ltr">I don't think submodules are automatically imported, unless there are import statements in __init__.py.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 10, 2017 at 8:08 PM, INADA Naoki <span dir="ltr"><<a href="mailto:songofacandy@gmail.com" target="_blank">songofacandy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It looks simple and easy to understand.<div><br></div><div>To achieve lazy import without breaking backward compatibility,</div><div>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).</div><div><br></div><div>For example, some modules depends on email.parser or email.feedparser.</div><div>But since email/__init__.py uses __all__, all submodules</div><div>are imported eagerly.</div><div><br></div><div>See <a href="https://github.com/python/cpython/blob/master/Lib/email/__init__.py#L7-L25" target="_blank">https://github.com/python/<wbr>cpython/blob/master/Lib/email/<wbr>__init__.py#L7-L25</a><br></div><div><br></div><div>Changing __all__ will break backward compatibility.</div><div>With __getattr__, this can be lazy import:</div><div><br></div><div>import importlib</div><div><br></div><div>def __getattr__(name):</div><div>    if name in __all__:</div><div>        return importlib.import_module("." + name, __name__)</div><div>    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")</div><div><br></div><div><br></div><div>Regards,</div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>