Given a directory with the following structure:
├── foo
│ ├── bar
│ │ └── baz.py
│ └── bar.py
└── foo.py
and the following file contents:
foo.py
class Foo:
pass
foo/bar.py
class Bar:
pass
foo/bar/baz.py
class Baz:
pass
I would love to be able to do all of these things from some other file:
from foo import Foo
from foo.bar import Bar
from foo.bar.baz import Baz
i.e. as if the definitions in each file were "merged" with the module definitions comprising the directory with the same name. How can I accomplish this in a dynamic way (whether involving __init__.py or otherwise) that won't require me to manually specify each path?
When I place empty __init__.py files in each directory and attempt to perform the above imports, I receive the following error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from foo import Foo
ImportError: cannot import name 'Foo'
which seems to me to suggest that the foo.py file has been overridden by the foo directory so that the definitions in foo.py are not visible.
foo.bar.baz? in which case yes this is fairly easy with an__init__.pyfile.__init__.pyfiles, for example, to include each new file/directory for which I want this behavior.