liveBook · Manning

__init__.py

As explained in Python Workout: 50 ten-minute exercises

__init__.py is the conventional initialization file whose execution initializes a Python package when the package is imported.

__init__.py is the file that makes importing a package meaningful: importing mypackage loads and executes mypackage/__init__.py. That file can import modules such as first.py, allowing package-level initialization and access. section 8.10

A package with modules

Given mypackage/first.py, mypackage/second.py, and mypackage/third.py, import mypackage loads __init__.py. A package can then be imported directly, or a module can be imported with from mypackage import first or import mypackage.first. section 8.10

from mypackage import first

Inspect the concise package-level form for importing a module from mypackage. section 8.10

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines initialization through executing __init__.py, but does not detail its contents beyond importing internal modules.
Examples
Uses mypackage with first.py, second.py, third.py, and several import forms.
What it assumes
Assumes familiarity with Python modules and imports, while distinguishing Python packages from distribution packages.
What you can do after
Apply it by placing __init__.py in a package and importing internal modules through package-level forms.
How it connects
Connects Python packages with modules, import statements, sys.path, and distribution packages.

Also explained in

Compare how 4 books explain __init__.py →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.