liveBook · Manning

__init__.py

__init__.py is a Python package module that may initialize a package’s namespace and behavior when the package is imported.

How 4 Manning books explain it

The Well-Grounded Python Developer: How the pros use Python and Flask

An __init__.py file is a Python package file that marks a directory as a package and may contain package initialization code.

How it is used
Organizes related Python modules into importable packages and supports a Flask application factory.
How deep it goes
Defines the file’s package role, explains its execution during imports, and works through utility and application layouts without exploring import internals.
Examples
Uses a string utility moved into a package and a Flask application reorganized around an app package.
What you can do after
Build it from scratch by adding __init__.py to a directory, then import its modules or expose a Flask app factory.

Read how The Well-Grounded Python Developer: How the pros use Python and Flask explains __init__.py →

The Quick Python Book, Fourth Edition

An __init__.py file is a Python package file that can contain initialization code executed when the package is imported.

How it is used
Uses __init__.py to mark package directories and run initialization when packages are first loaded.
How deep it goes
Explains first-load execution, package attributes, nested imports, and __all__ through examples, without detailing import machinery internals.
Examples
Demonstrates initialization messages, a package version attribute, nested imports, and wildcard imports with a small package tree.
What you can do after
Build it from scratch: create an empty or populated __init__.py, then choose explicit imports or __all__ for package access.

Read how The Quick Python Book, Fourth Edition explains __init__.py →

Python Workout: 50 ten-minute exercises

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

How it is used
Initializes a Python package when importing the package and can expose its internal modules.
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 you can do after
Apply it by placing __init__.py in a package and importing internal modules through package-level forms.

Read how Python Workout: 50 ten-minute exercises explains __init__.py →

Python Workout, Second Edition

__init__.py is a conventional Python package file whose code is executed when the containing package is imported.

How it is used
Groups related Python modules and runs package initialization code during package imports.
How deep it goes
Explains package recognition, initialization, and module imports, but does not detail execution order or advanced package configuration.
Examples
Uses mypackage with first.py, second.py, third.py, and alternative import statements.
What you can do after
Build it from scratch by organizing modules into a package and selecting direct or qualified import forms.

Read how Python Workout, Second Edition explains __init__.py →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.