Namespace Package
Overview
Namespace packages are a powerful feature in Python that allow developers to split a single distribution package into multiple distribution packages while maintaining a unified namespace. This is particularly useful for large projects or when different parts of a project are developed and distributed separately. Unlike regular packages, namespace packages do not contain their own __init__.py module. This absence allows multiple directories, potentially located in different places, to share a common name and act as a single namespace.
Figure 10.3 Namespace packages break up an existing distribution package into multiple distribution packages while maintaining a single, top-level namespace.
Structure and Usage
The structure of a namespace package is different from that of a regular package. In a namespace package, the directory acts as a container for other packages but does not itself contain an __init__.py file. This allows the directory to be part of a larger namespace that spans multiple directories.
Example Directory Structure
Consider the following directory structure, which demonstrates a namespace package:
Listing 10.9 A directory structure with a single namespace package
├── geometry-lines
│ └── geometry #1
│ └── lines #2
│ └── __init__.py
└── geometry-polygons
└── geometry #3
└── polygons #4
└── __init__.py
#1 A namespace package providing packages in the geometry namespace
#2 Importable as geometry.lines
#3 Another namespace package providing packages in the geometry namespace
#4 Importable as geometry.polygons
In this example, both geometry-lines and geometry-polygons directories contribute to the geometry namespace. The lines and polygons packages can be imported as geometry.lines and geometry.polygons, respectively.
Converting Existing Packages
To convert an existing package into a namespace package, you need to modify the directory structure by removing the __init__.py file from the directory that will become the namespace package. Additionally, update the [options] section in the setup.cfg file by changing the packages key from find: to find_namespace: and adding a namespace_packages key with the new namespace’s name.
Namespace vs. Regular Packages
The key difference between namespace packages and regular packages is the presence of the __init__.py file. Regular packages contain this file, which makes them self-contained and gives them precedence over namespace packages when Python resolves imports. If a directory is a regular package in one location but a namespace package in another, Python will prioritize the regular package.
Precedence Example
Listing 10.10 A regular package taking precedence over a namespace package
├── geometry-lines
│ └── geometry
│ ├── __init__.py #1
│ └── lines
│ └── __init__.py
└── geometry-polygons
└── geometry
└── polygons #2
└── __init__.py
#1 Makes geometry a regular package and takes precedence
#2 No longer importable if both namespace packages are on the path
In this scenario, the presence of an __init__.py file in the geometry directory of geometry-lines makes it a regular package, which takes precedence over the namespace package. As a result, geometry.polygons becomes non-importable if both packages are on the path.
Nested Namespace Packages
Namespace packages can also be nested, but the structure must be consistent across different directories. If a directory is a namespace package in one location but a regular package in another, the regular package will take precedence, and the namespace package will not function as intended.
FAQ (Frequently asked questions)
What is a namespace package in Python?
How do namespace packages differ from regular packages?
What is the purpose of Figure 10.3 in the context of namespace packages?
What does the directory structure in Listing 10.9 represent?
How can you import packages from a namespace package?
Why do namespace packages not contain an __init__.py file?
What takes precedence when Python resolves imports, regular packages or namespace packages?
How do you convert an existing package to a namespace package?