How do I create a namespace package in Python?


In Python, a namespace package is a technique to split a single Python package across multiple directories or even several computers. It allows developers to distribute functionality across multiple packages and make it easier to maintain.

A namespace package is a package that doesn't have an init .py file but instead depends on other packages to provide it.

A namespace package in Python allows you to spread the code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories Package-1 and Package-2 in PYTHONPATH,

Package-1/namespace/ init .py Package-1/namespace/module1/ init .py Package-2/namespace/ init .py
Package-2/namespace/module2/ init .py	

the end-user can import namespace.module1 and import namespace.module2.

On Python 3.3, all you need to do is leave out any init .py files from your namespace package directories, and everything will operate as it should. This is because Python 3.3 introduces implicit namespace packages.

On older versions, there's a standard module, called pkgutil, with which you can 'append' modules to a given namespace. You should put those two lines in both Packages.

1/namespace/__init__.py and Package-2/namespace/__init__.py:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

This will add to the package's path__ all subdirectories of directories on sys.path named after the package. After this you can distribute the 2 packages separately.

Here are some of the ways you can create namespace packages in Python −

Basic Namespace Package

The simplest way to create a namespace package is to create a directory with the desired name and no init .py file.

Example

#create a directory in linux with desired name
mkdir my_namespace/module1.py
#You can then import the module from the namespace package like this:
from my_namespace import module1

Multiple Directories

You can split a namespace package across multiple directories by creating multiple directories with the same name and no __init__.py file.

Example

 my_namespace/
dir1/
module1.py
dir2/
module2.py
#You can then import modules from the namespace package like this:
from my_namespace.dir1.module1 import my_function1
from my_namespace.dir2.module2 import my_function2

Namespace Package with pkgutil

You can create a namespace package with pkgutil by creating a directory with the desired name and adding a pkgutil.py file with the following code:

Example

import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
my_namespace/
pkgutil.py
module1.py
#You can then import the module from the namespace package like this:
from my_namespace.module1 import my_function

Namespace Package with pkg_resources

You can create a namespace package with pkg_resources by creating a directory with the desired name and adding an empty __init__.py file and a setup.py file with the following code:

Example

from setuptools import setup, find_namespace_packages
setup(
   name="my_namespace",
   packages=find_namespace_packages(include=["my_namespace.*"])
)
my_namespace/
   __init__.py
   setup.py
   module1.py

You can then install the namespace package with pip install and import the module from the namespace package like this:

from my_namespace.module1 import my_function

Namespace Package with namespace_packages in setuptools

You can create a namespace package with setuptools by creating a directory with the desired name and adding an empty __init__.py file and a setup.py file with the following code:

Example

from setuptools import setup, find_namespace_packages
setup(
   name="my_namespace",
   packages=find_namespace_packages(include=["my_namespace.*"])
)
my_namespace/
   __init__.py
   setup.py
   module1.py

You can then install the namespace package with pip install and import the module from the namespace package like this:

from my_namespace.module1 import my_function

Namespace Package with pkg_resources and namespace_packages

You can create a namespace package with both pkg_resources and namespace_packages by creating a directory with the desired name and adding an empty __init__.py file and a setup.py file with the following code:

Example

from setuptools import setup
from setuptools import find_packages
from pkg
import NamespacePackage
setup(
   name="my_namespace",
   packages=find_packages(),
   namespace_packages=["NamespacePackage"]
)
NamespacePackage/
   my_namespace/
   init.py
   module1.py
   setup.py

You can then install the namespace package with `pip install ` and import the module from the namespace package like this:

```python
from NamespacePackage.my_namespace.module1 import my_function

To summarize, these are some of the ways to create namespace packages in Python. They allow you to distribute functionality across multiple packages and make it easier to maintain.

Updated on: 02-May-2023

812 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements