Sub topic 10
Designing a Package's Public Interface with __all__
This chapter assumes you can write a package with __init__.py and submodules, and that you know relative imports. Here you decide which names a package exposes and which stay internal.
Why a public interface matters
Users of shapes should not need to know that area lives in shapes/circle.py. If they write from shapes.circle import area, moving that function later breaks their code. A package's __init__.py can present a stable surface, so internal files can change without breaking callers.
Re-exporting from `__init__.py`
The __init__.py file runs when the package is first imported. Names bound there become attributes of the package.
# shapes/__init__.py
from .circle import area as circle_area
__all__ = ["circle_area"]After this, import shapes followed by shapes.circle_area(2) works. The alias gives the function a clearer name at package level. The submodule shapes.circle is also loaded, and it remains reachable as shapes.circle.
What `__all__` controls
__all__ is a list of strings naming the public names of a module or package. It affects one thing: what from shapes import * imports.
- Without
__all__, a star import binds every name that does not start with an underscore. - With
__all__, a star import binds exactly the listed names, including ones that start with an underscore. - A name listed in
__all__that does not exist raisesAttributeErrorduring the star import.
__all__ does not block access. shapes._helper and shapes.circle.area still work. Python marks privacy by convention only: a leading underscore means "internal", and __all__ means "this is the supported API".
A package-level view
Common mistakes
- Forgetting to update
__all__. Add a new public function and the star import silently omits it. - Listing a name that is never imported.
__all__ = ["circle_area"]with no matching import fails onfrom shapes import *. - Treating
__all__as security. It is documentation plus star-import control, nothing more. - Importing everything eagerly. Re-exporting from many heavy submodules makes
import shapesslow, because each import runs the submodule's code.
Checking the result
Create a script outside the package that runs from shapes import * and then print(circle_area(1)). It should print 3.14159. Now add "missing" to __all__ and rerun it. You should see an AttributeError for missing.
Exercise
A package's __init__.py defines __all__ = ["circle_area"] and also imports area as _raw_area. After from shapes import *, can you call circle_area? Can you reach shapes._raw_area?
Show answer
Yes to circle_area, because it is listed in __all__. _raw_area is not bound by the star import, but import shapes followed by shapes._raw_area still works, because __all__ does not restrict attribute access.
Exercise
Why might you re-export area as circle_area in __init__.py instead of asking users to import shapes.circle?
Show answer
The package-level name stays stable if you move or rename the internal module, so user code does not break. The alias can also give a clearer name at the package level.