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:

Packages within packages

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 ...

Get Modular Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.