<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Apr 22, 2015 at 8:59 AM, Barry Warsaw <span dir="ltr"><<a href="mailto:barry@python.org" target="_blank">barry@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">[...]<br>
As it turns out, it's not the from-import-* that does the name binding, it's<br>
the importing of submodules.  Use any other submodule import spelling to make<br>
it work.  This includes<br>
<br>
    import spam.foo<br>
    from spam.foo import Foo<br>
    __import__('spam.foo')<br>
    importlib.import_module('spam.foo')<br>
<br>
Poking around in Lib/importlib/_bootstrap.py, I think you can see where this<br>
happens.  In _find_and_load_unlocked(), 'round about line 2224 (in 3.5's<br>
hg:95593), you see this:<br>
<br>
    if parent:<br>
        # Set the module as an attribute on its parent.<br>
        parent_module = sys.modules[parent]<br>
        setattr(parent_module, name.rpartition('.')[2], module)<br>
<br>
It's clearly intentional, and fundamental to importlib so I don't think it's<br>
dependent on finder or loader.  No matter how it happens, if a submodule is<br>
imported, its parent namespace gets a name binding to the submodule.<br>
<br>
What was the motivation for this?  Was the intent really to bind submodule<br>
names in the parent module seemingly magically?<br>
<br>
AFAICT, this also isn't actually documented anywhere.  I've looked in the<br>
Language Reference under the import system[*], and import statement, nor in<br>
the Library Reference under __import__().  There's lots of material here, so I<br>
could be missing it.<br>
<br>
I don't know whether any of the alternative implementations also implement<br>
this behavior, but they'll have to.<br>
<br>
I think this needs to be documented in the Language Reference, and after some<br>
feedback here, I'll open a docs bug and write some text to fix it.<br></blockquote></div><br></div><div class="gmail_extra">It's definitely intentional, and it's fundamental to the package import design. We've had many implementations of package import (remember "ni.py"? last seen as "knee.py") and it was always there, because this is done as part of *submodule loading*. For better or for worse (and because I didn't know Java at the time :-) Python declares that if you write `import foo.bar` then later in your code you can use `foo.bar` to reference to the bar submodule of package foo. And the way this is done is to make each submodule an attribute of its parent package. This is done when the submodule is first loaded, and because of the strict separation between loading and importing, it is done no matter what form of import was used to load bar.<br><br></div><div class="gmail_extra">I guess another thing to realize is that the globals of __init__.py are also the attribute namespace of the package.<br><br></div><div class="gmail_extra">I'm not surprised it's in the reference manual -- that hasn't been updated thoroughly in ages, and I sometimes cry when I see it. :-) So please do clarify this for the benefit of future implementers.<br> </div><div class="gmail_extra"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>