Python - Namespace Package

> Procedural Languages > Python > Python - Grammar (Package | Module)

1 - About

Namespace packages allow you to split the sub-packages and modules within a single package across multiple, separate distribution packages

Advertising

3 - Example

mynamespace/
    __init__.py
    subpackage_a/
        __init__.py
        ...
    subpackage_b/
        __init__.py
        ...
    module_b.py
setup.py

And you use this package in your code like so:

from mynamespace import subpackage_a
from mynamespace import subpackage_b

Then you can break these sub-packages into two separate distributions:

mynamespace-subpackage-a/
    setup.py
    mynamespace/
        subpackage_a/
            __init__.py

mynamespace-subpackage-b/
    setup.py
    mynamespace/
        subpackage_b/
            __init__.py
        module_b.py

Each sub-package can now be separately installed, used, and versioned.

4 - Management

4.1 - Packaging

In the Python - setup.py,

setup.py
packages=find_packages(),
namespace_packages=['subPackage1', 'subPackage2', '...', 'subPackage3'],
lang/python/grammar/namespace_package.txt · Last modified: 2018/12/03 12:56 by gerardnico