https://github.com/jcrist/namespace_modules
Example of using namespace modules
https://github.com/jcrist/namespace_modules
Last synced: 3 months ago
JSON representation
Example of using namespace modules
- Host: GitHub
- URL: https://github.com/jcrist/namespace_modules
- Owner: jcrist
- Created: 2017-02-08T18:11:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-08T18:11:44.000Z (over 8 years ago)
- Last Synced: 2025-04-14T19:47:35.760Z (6 months ago)
- Language: Python
- Size: 1000 Bytes
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Namespace modules in python
A quick example of how namespace modules work in both python 2 & 3.
## Overview
- ``daskit``
The root namespace module. This could be distributed as part of the main
``dask`` project.- ``daskit.foo``
A separate package that's installed under the main ``daskit`` namespace.
- ``daskit.bar``
A separate package that's installed under the main ``daskit`` namespace.
## What a package needs to do:
- Have a top-level folder matching the namespace name (in this case ``daskit``)
- Include this line (and only this line) in the top-level ``daskit/__init__.py``
```python
__import__("pkg_resources").declare_namespace(__name__)
```- Include a subfolder under ``daskit`` with all the module code. This can be a
regular package.- A ``setup.py`` looking something like:
```python
from setuptools import setupsetup(name='your_package_name_on_pypi',
packages=['daskit.your_package_name'],
namespace_packages=['daskit'],
zip_safe=False)
```## Example
After installing the packages above, a user session might look like:
```python
In [1]: import daskit
# Nothing in the top-level namespace
In [2]: [mod for mod in dir(daskit) if not mod.startswith('_')]
Out[2]: []In [3]: import daskit.foo
# foo is now in the top-level namespace
In [4]: [mod for mod in dir(daskit) if not mod.startswith('_')]
Out[4]: ['foo']In [5]: daskit.foo.foo(1, 2)
calling from dask.foo
Out[5]: 3In [6]: from daskit import foo, bar # Import submodules
In [7]: foo.foo(1, 2)
calling from dask.foo
Out[7]: 3In [8]: bar.bar(1, 2)
calling from daskit.bar
Out[8]: 3
```