liveBook · Manning

__init__.py

As explained in 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.

A growing Python application needs more than descriptive filenames: related modules also need a clear place in the directory hierarchy. Add an __init__.py file to a directory to make it a package, so its modules can be imported through a meaningful namespace. section 2.3.2

Give a utility a clear home

Suppose a string-handling add() function lives in utility.py. Move it to utilities/strings.py, add an empty utilities/__init__.py, and import it with from utilities import strings. Calling strings.add(12, 12) makes the function’s purpose clearer than an unqualified add(). section 2.3.2

from utilities import strings
 
def add(a, b):
    return a + b
 
print(add(12, 12))
print(strings.add(12, 12))

The import and call show how the package and module names make the string-handling function distinct from a local add() function. section 2.3.2

The file can stay empty, but it need not. Python executes code in __init__.py when importing through the package. That makes it a possible home for package-level code, not just a marker in the file tree. section 2.3.2 section 7.3.1

The same pattern helps organize a Flask application. An app directory with __init__.py becomes a package; its initializer can provide a create_app() factory, which a separate entry-point module imports to create the Flask app instance. Packages can also be nested when the application needs a deeper hierarchy. section 7.3.1

├── app
│   ├── __init__.py
│   ├── static
│   │   ├── css
│   │   │   └── base.css
│   │   ├── images
│   │   │   ├── myblog_banner.png
│   │   │   └── myblog_banner_50.png
│   │   └── js
│   │       └── index.js
│   └── templates
│       ├── base.html
│       └── index.html
└── myblog.py

The directory tree places __init__.py at the package root alongside the application’s static assets and templates. section 7.3.1

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

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 it assumes
Assumes familiarity with Python functions and imports while introducing package creation from an empty __init__.py file.
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.
How it connects
Connects __init__.py to Python packages, modules, namespaces, imports, and the Flask application factory pattern.

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 The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.