18.3 Packages
Python allows you to encapsulate multiple modules in one package. This is advantageous if these modules belong together thematically. In contrast to a single module, a package can contain any number of other packages, which in turn can contain modules or packages.
To create a package, a subfolder must be created in the program directory. The name of the folder must correspond to the name of the package. Additionally,[ 86 ] this folder can contain a program file named __init__.py (note the two underscores both before and after init here). This file contains initialization code that’s executed once when the package is imported.
Note
If a package and a module with the same name exist in the same directory, this will cause a name ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access