Init .Py
Init .Py
__init__.py
Ammar Munir
WHAT ?
The __init__.py file is used in Python to mark a directory as a package. A package
is a way of organizing related modules into a single directory hierarchy. By
including an __init__.py file in a directory, Python treats the directory as a
package, allowing you to import modules from it.
BUT !
Without an __init__.py file, a directory is not considered a package in older
versions of Python (before 3.3). However, starting from Python 3.3, implicit
namespace packages were introduced, which allow directories to be recognized
as packages without needing an __init__.py file.
Ammar Munir
Here are the reasons why we still use
__init__.py in python3.3+
Ammar Munir
Structure Initializing the Package
package-wide variables,
__init__.py
importing specific
submodules, or executing
main.py
package-level code.
output.py
Ammar Munir
Structure
Controlling imports
Module2
__init__.py
be accessible.
main.py
Ammar Munir
Structure
Providing a convenient interface
Module1
selected functions or
level.
__init__.py
main.py
Ammar Munir
Structure Ensuring compatibility and clarity
intended to be a package,
compatibility and
understanding the
main.py
package structure.
Ammar Munir
Was it helpful ?
Ammar Munir
Python | Django | Web Engineer