Module 14 · Python Modules and Packages — topic 8 of 13
__init__.py
The role of __init__.py: marking a folder as a regular package, running package initialisation code, exposing a convenient public API, __all__ and __version__, and why submodules aren't imported automatically.
Learning objectives
- Explain when __init__.py runs
- Expose a package's public API from __init__.py
- Know that submodules must be imported explicitly
Definition
__init__.py is a module that runs when its package is imported. It marks the folder as a regular package and can define package-level names, import submodules to build a convenient interface, and set __all__ and __version__.
Introduction
Users of your package shouldn't need to know your internal file layout. __init__.py lets them write geometry.circle_area instead of geometry.circle.area.
Why it matters
A well-designed __init__.py gives a package a clean front door and lets you reorganise internals without breaking users.
Detailed explanation
import package runs package/__init__.py once (and caches the package like any module). An empty file is perfectly fine.
Names defined or imported in __init__.py become attributes of the package: from .circle import area as circle_area makes package.circle_area available.
Importing a package does not automatically import its submodules. After import tools, tools.text exists only if __init__.py imports it or you import tools.text yourself.
__all__ in __init__.py controls from package import *.
__version__ = "1.2.0" is a common convention for exposing the package version.
Keep __init__.py light: heavy work there slows every import of the package and can cause circular-import problems.
Without __init__.py, a folder becomes a namespace package. That works, but regular packages with __init__.py are clearer for normal projects.
Syntax
- Runs when the package is imported.
- Re-export names for a clean API.
- Keep it light.
Code examples
A package with a public API
initialising geometry 3.14 9 1.2.0
Submodules need importing
False True HI!
More examples
A dispatching package interface
pen,20 / bag,1200 [pen] [20] / [bag] [1200] unknown format 'pdf'; choose from ['csv', 'text'] ['to_csv', 'to_text']
Step-by-step explanation
- __init__.py imports the two format functions and builds a FORMATS table.
- Users call reports.render without knowing which file implements each format.
- from reports import * brings in exactly the names listed in __all__.
Expected output
Running “A dispatching package interface” prints exactly:
Practical applications
- requests/__init__.py re-exports get, post and Session.
- Packages exposing __version__ for tools and bug reports.
Best practices
- Re-export the public API from __init__.py.
- Define __all__ and __version__.
- Keep __init__.py fast and free of side effects.
Common mistakes
Expecting automatic submodule import
Avoid
Prefer
Submodules load only when imported (by you or by __init__.py).
Heavy initialisation
Avoid
Prefer
Every import of the package would pay the cost.
Important notes
- Use relative imports (from .module import name) inside __init__.py.
Key takeaways
- __init__.py runs when the package is imported.
- It can re-export names for a clean API.
- Submodules aren't imported automatically.
Try it yourself
- 1Create a units package whose __init__.py exposes km_to_miles and kg_to_lb from two submodules.
- 2Add __version__ to a package and print it from a script.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateAfter import shop, why might shop.pricing raise AttributeError?