3 ms·
Per the example (the services/component_b/child example) in the article, are you supposed to put __init__.py in the sub-folder of a module too ? I'm asking bec
by thrdbndndn 2y ago
Per the example (the services/component_b/child example) in the article, are you supposed to put __init__.py in the sub-folder of a module too ?
I'm asking because I rarely see this be done (nor have I).
- bastawhiz 2y agoYes, any folder that contains files that are expected to be imported (or other folders that have the same)
- zahlman 2y agoYes, it goes in every "regular package" folder (a folder without one creates a "namespace package" - or rather, a "portion" thereof; this allows for a parallel file hierarchy somewhere else that holds another part of the same package). Python represents packages as `module` objects - the exact same type, not even a subclass. These are created to represent either the folder or an `__init__.py` file. The file need not be empty and in fact works like any other module, with the exception of some special meaning given to certain attributes (in particular, `__all__`, which controls the behaviour of star-imports). Notably, you can use this to force modules and subpackages within the package to load when the package is imported, even if the user doesn't request them (`collections.abc` does this); and you can make some other module appear to be within the package by assigning it as an attribute (`os.path` works this way; when `os` is imported, some implementation module such as `ntpath` or `posixpath` is chosen and assigned).