Python › Modules & Packaging

What is a namespace package, and how does it differ from a regular package?

Advanced

Answer

A namespace package (PEP 420) is a package without an __init__.py that can span multiple directories or distributions -- Python implicitly recognizes any directory containing Python files as a namespace package portion, letting a single logical package (like `myorg.tools`) be split across separately installed distributions that merge into one importable namespace at runtime.
# No __init__.py needed:
# dist1/myorg/tools/a.py
# dist2/myorg/tools/b.py
# Both contribute to the same 'myorg.tools' namespace package

import myorg.tools.a
import myorg.tools.b
Real-world example A large organization splits a monorepo's internal tooling into separately versioned pip packages (myorg-tools-network, myorg-tools-storage) that all contribute submodules to the same `myorg.tools` namespace.

Common follow-ups: What are the downsides of namespace packages regarding import speed?;How do you explicitly declare a regular package to avoid ambiguity?