__init__.py
__init__.py is a Python package module that may initialize a package’s namespace and behavior when the package is imported.
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.
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
- packageconfigures · A software package is a named grouping of modules or components, often with related resources and metadata, organized for import, reuse, distribution, or management.
- packageconfigures · A package is a programming-language namespace and organizational unit that groups related code and may define import, compilation, or access boundaries.
- side effectproduces · A software side effect is an observable change or interaction caused by an operation beyond producing its direct result, such as modifying state or performing input or output.
- file objectkind of · A file object is a program-accessible representation of an open file or stream, including its access state and input/output operations.
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.



