In Python, namespace packages are a convenient approach for distributing related modules. They enable multiple Python products to define modules within the same namespace, thereby allowing end-users to import them seamlessly.
Beginning with Python 3.3, the concept of implicit namespace packages was introduced. This eliminates the need for explicit __init__.py files within namespace package directories. The importing system automatically searches sys.path for directories without __init__.py files and initializes them as namespace packages.
For Python versions prior to 3.3, the pkgutil.extend_path() method can be utilized to define explicit namespace packages:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
This line adds any regular packages found in the search path to the path attribute of the namespace package.
In scenarios where both regular and namespace packages coexist, the extend_path() method mentioned earlier has been extended to include implicit namespace packages. This allows for the following directory structure:
├── path1 │ └── package │ ├── __init__.py │ └── foo.py ├── path2 │ └── package │ └── bar.py └── path3 └── package ├── __init__.py └── baz.py
With __init__.py files employing extend_path(), import package.foo, import package.bar, and import package.baz will all function as expected.
In contrast to pkgutil.extend_path(), the pkg_resources.declare_namespace() function has not been updated to support implicit namespace packages. Therefore, it is advisable to employ the pkgutil solution.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3