Suppose we have the following directory structure
foo/
__init__.py
bar.py
import foo
runs foo/__init__.py
(see Regular Package). import foo.bar
imports the file bar.py
as a module bar
Question
Why do we have a
foo
module which is a folder and abar
module which is a file?
Well actually, foo
is a package and bar
is a module in it. It starts to make sense if you consider that the file bar.py
is growing and we want to split it into multiple files. Then, we can make it into a package! The directory structure will then look like:
foo/
__init__.py
bar/
__init__.py
baz1.py
baz2.py
All the things that bar.py
exposed earlier can now be kept inside bar/__init__.py
. This makes the change transparent to all users who import bar
.