Namespace Packages (PEP 420 Split Packages)
1Concept
PEP 420 introduced implicit Namespace Packages in Python 3.3+. Directories without `__init__.py` can share a common top-level package namespace across multiple separate project repositories and installation paths (e.g. `azure.storage` and `azure.identity`).
2Architecture Diagram
Repo 1: company/service_a/core.py Repo 2: company/service_b/core.py Client imports: `import company.service_a` and `import company.service_b` seamlessly!
3Code Example
Python 3.12
import sys
from pathlib import Path
# Inspecting namespace package characteristics
print("Namespace Package Rule (PEP 420):")
print("- Omit __init__.py across split directories.")
print("- CPython dynamically creates a _NamespacePath spanning multiple directories.")4Expected Output
Namespace Package Rule (PEP 420): - Omit __init__.py across split directories. - CPython dynamically creates a _NamespacePath spanning multiple directories.
5Key Takeaways
- ✓Namespace packages lack `__file__` attribute because they do not correspond to a single file.
- ✓Their `__path__` is a dynamic iterable (`_NamespacePath`) rather than a simple list.
- ✓Standard practice for large monorepos and multi-repo enterprise SDK suites.