
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
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 summarise, 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.
- Related Articles
- How do I create a namespace package in Python?
- How do I create a Python namespace?
- How do I import all the submodules of a Python namespace package?
- How to import everything from a python namespace / package?
- How do I declare a namespace in JavaScript?
- How to create python namespace packages in Python 3?
- How do I write package names in Java?
- How do I create a multidimensional list in Python?
- How do I create a .pyc file in Python?
- Do recursive functions in Python create a new namespace each time the function calls itself?
- How do I create a user interface through Python?
- Create and Access a Python Package
- How do I create documentation from doc strings in Python?
- How do I create child windows with Python tkinter?
- How do I create a view in MySQL?
- How do I create a java.sql.Date object in Java?
