Sub topic 7
Packages and __init__.py
This chapter assumes you can write functions and classes, run scripts from a terminal, and that you know how a single file becomes a module and how sys.path is searched. Here we move from one file to a directory of files.
What a package is
A package is a module that can contain other modules. On disk it is a directory. Python treats a directory as a regular package when it contains a file named __init__.py. When you import the package, Python runs that file once and binds the result to the package name, in the same way it runs a module file.
Here is the running example for this chapter:
shapes/
__init__.py
circle.py
util/
__init__.py
mathx.pyshapes is a package. shapes.circle is a module inside it. shapes.util is a subpackage, and shapes.util.mathx is a module inside that subpackage.
What `__init__.py` does
The file has three common uses:
- It marks the directory as a regular package.
- It runs initialization code the first time the package is imported.
- It defines the names that users see directly on the package.
An empty __init__.py is valid. It is common when you only want the directory to be importable.
Importing a package does not import its submodules automatically. Only the code in __init__.py runs. A submodule loads when you import it, or when __init__.py imports it.
import shapes
print(shapes.__name__)
print(hasattr(shapes, "circle"))With an empty __init__.py, the first line prints shapes and the second prints False, because circle has not been imported. After import shapes.circle, the name circle is bound as an attribute of shapes.
Re-exporting names
You can import from submodules inside __init__.py so users do not need to know your internal file layout:
# shapes/__init__.py
from .circle import area as circle_area
__all__ = ["circle_area"]Now from shapes import circle_area works. The leading dot means "the package that contains this file", so .circle is shapes.circle. The __all__ list controls which names from shapes import * imports. Relative imports and __all__ get their own chapters, so treat this as a preview.
flowchart TDThe diagram above is replaced by the one below, which shows what happens on import.
The parent package always loads before its child. This means code in shapes/__init__.py runs before anything in shapes/circle.py.
Common errors
- Missing
__init__.pyin older tooling. Without it, Python 3 may still import the directory as a namespace package, but some tools and test runners will not find your code. Add the file unless you want a namespace package on purpose. - A heavy
__init__.py. Every import of any submodule runs the package's__init__.pyfirst. Slow or side-effect-heavy code there slows every import. - Name clashes. If
__init__.pydefines a name equal to a submodule name, such as a function calledcircle, the function replaces the submodule attribute on the package.
Exercise
You create a directory tools/ containing __init__.py (empty) and parse.py. After running only import tools, does tools.parse exist? What do you need to change so that it does?
Show answer
No. An empty __init__.py does not import parse, so tools.parse raises AttributeError. Either run import tools.parse, or add from . import parse to tools/__init__.py.