then run it:
$ python2
>>> import foo
>>> import foo.bar
TEST OK
>>>
There is no other way to do this.
Changing the line to import foo.bar.baz as baz ends in this error:
AttributeError: 'module' object has no attribute 'bar'
And you cannot use foo.bar.baz because this does not exists because the module is not yet appended to foo.bar;
$ cat foo/bar/__init__.py
import foo.bar.baz
baz.test()
foo.bar.baz.test()
$ python
>>> import foo
TEST OK
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./foo/__init__.py", line 1, in <module>
import foo.bar
File "./foo/bar/__init__.py", line 4, in <module>
foo.bar.baz.test()
AttributeError: 'module' object has no attribute 'bar'
With the first line i can access the function, the other one not!
I can't decide if this is a legitimate thing to do, or something that just happens to work due to some bug or implementation detail in cPython.
The language specification <https://docs.python.org/2/reference/simple_stmts.html#the-import-statement> doesn't seem to be much help. It talks a lot about how the import statement finds the thing to import, but then step 2, binding it to some local variables, there's pretty much no detail. I don't see anywhere in that specification to *what* local variables anything is bound in the case of your example.
Phrased in the terms of your example, it doesn't say anything about how one might do "import foo.bar.baz" and end up with "baz" defined. But as your test environment demonstrates, and I've been able to reproduce, it does.
Can you cite any normative references that provide additional clarity?
Dave Beazley mentioned this in his modules tutorial (https://www.youtube.com/watch?v=0oTh1CXRaQ0, see around 32:00 or so; I recommend watching the whole thing if you can find the time). The gist of it is that because echo is a sub-module/package of sound.effects, you can type sound.effects.echo. The way that Python implements this is by literally putting "echo" in the namespace of sound.effects (i.e., its __init__.py). That's why import foo.bar.baz pulls in the name "baz" **in foo/baz/__init__.py**.
Dave doesn't touch on this any more than that once in his tutorial as I recall, so you may want to look for some more references on how the import system works if it's still unclear.
So this also works on 3.4 (which makes me assume it has worked on prior 3.x versions) but I haven't tested 3.5. Further if you toss a
print(globals())
Into foo/bar/__init__.py, you'll see very clearly that `baz` is defined as the submodule.
While I don't think this is conventional python or even common to see, it makes sense to support. For what it's worth, I would expect something more akin to
from . import baz
Inside foo/bar/__init__.py instead of
import foo.bar.baz
That said, I know many people follow the Google Styleguide and have linters that enforce that meaning they have to use the latter.
The use of
from . import baz
instead of
import foo.bar.baz
is not practicable/considerable:
It is not possible to use the "if __name__ == '__main__'" -feature then to call that script also directly. It breaks with:
"ValueError: Attempted relative import in non-package"
hello,
your import looks weird, btw. I believe it's wrong.
IMHO it should read:
from foo.bar import baz
baz.blah()