What Is Python’s __init__.py For?
Python’s special __init__.py
file marks a directory as a regular Python package and allows you to import its modules. This file runs automatically the first time you import its containing package. You can use it to initialize package-level variables, define functions or classes, and structure the package’s namespace clearly for users.
By the end of this tutorial, you’ll understand that:
- A directory without an
__init__.py
file becomes a namespace package, which behaves differently from a regular package and may cause slower imports. - You can use
__init__.py
to explicitly define a package’s public API by importing specific modules or functions into the package namespace. - The Python convention of using leading underscores helps indicate to users which objects are intended as