Packages within packages
Just like you can have directories within directories, you can have packages within other packages. For example, imagine that our my_package
directory contained another directory called my_sub_package
, which itself had an __init__.py
file:
As you might expect, you import the modules within a sub-package by prepending the names of the packages that contain it:
from my_package.my_sub_package import my_module my_module.do_something()
There is no limit to how deeply you can nest packages, though in practice it becomes a bit unwieldy if you have too many levels of packages-within-packages. More interestingly, the various packages and sub-packages form a tree-like structure which allows you to organize even the most complex program. For example, a sophisticated business system might be arranged like this:
As you can see, this is called a tree-like structure because the packages-within-packages look like the spreading branches of a tree. A tree-like structure like this allows...