Python Package Structure: Experts Demand `__init__.py` or Face Broken Imports
For directory structures to function as Python packages, the system requires an `__init__.py` file inside the target directory. Furthermore, internal module dependencies must use relative import statements, such as `from .module import function`, to ensure proper package context.
The conflict centers on managing namespace pollution within that `__init__.py`. Some users, like 'dgdft' and 'AstroLightz', push to explicitly list every public name in `__all__` to make top-level imports clean. 'logging_strict' actively warns against this, pointing to SQLAlchemy as evidence and citing anticipated stability from Python 3.15's 'soft import' feature.
The technical weight tips toward established structure: directories need `__init__.py`, and relative imports are mandatory. However, the debate over aggressive namespace control versus relying on future Python features remains the core fault line.
Key Points
Directory necessity requires an `__init__.py` file.
Multiple contributors stressed this fundamental requirement for directories to act as packages.
Internal imports must use relative notation.
This is the mandated method for managing local dependencies within a package structure.
Explicitly listing public names in `__all__` simplifies imports.
'dgdft' and 'AstroLightz' advocated for this explicit mechanism to achieve clean top-level name exposure.
Aggressive top-level imports risk performance and stability.
'logging_strict' cautioned against this practice, preferring to wait for better language features like Python 3.15 soft imports.
Complex applications benefit from layered module organization.
'rtxn' proposed a structured pattern using a primary `lib` directory, demonstrating advanced parent package imports (`..`).
Source Discussions (3)
This report was synthesized from the following Lemmy discussions, ranked by community score.