apipkg
apipkg: namespace control and lazy-import mechanism
What it is and what it does
apipkg is a small pure Python module that lets you control which names your package exports and when they are imported. Instead of importing everything upfront in your __init__.py, you define a namespace dictionary mapping public names to their import locations as strings, and apipkg defers the actual import until someone accesses that name. This means your package's startup is faster, users only pay the cost of importing what they actually use, and your public API is clearly defined in one place.
The package works by intercepting attribute access on your module and performing the import on demand. It integrates with Python's help() system and command-line completion tools, so users get the same introspection experience as with regular imports. Since it has no runtime dependencies and is only ~200 lines of code, you can also copy it directly into your project if you want to avoid an external dependency.
Use it for:
- Define a clean public API for a package with many internal modules without forcing all imports at startup.
- Reduce import time for packages with heavy dependencies by deferring their loading until needed.
- Organize a large package's namespace hierarchically while keeping the underlying module structure flexible.
- Simplify user code by exposing deeply nested classes or functions at the top level of your package.
- Build plugin systems where submodules are only loaded when explicitly requested.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
apipkg lets you define a package's exported namespace declaratively and defer imports until they are accessed, reducing startup overhead and simplifying your public API.
Yes. apipkg solves a real problem—controlling package namespaces and deferring imports—with zero dependencies, active maintenance, and permissive licensing. It's particularly valuable if you're building a library with many internal modules or heavy dependencies. The code is simple enough to embed if you want to avoid the dependency, but installing it is low-friction and gives you a battle-tested implementation.
Install
apipkg on PyPI
pip
pip install apipkguv
uv add apipkgpoetry
poetry add apipkgInstalling apipkg
Before you install
Low friction: pure Python with no runtime dependencies, distributed as a wheel, and actively maintained. Last commit was 2026-08-10 and the repository is not archived.
License in practice
MIT-licensed under permissive terms, so you can use it freely in commercial or open-source projects without restriction.
Quickstart
pip install apipkg
# In your package's __init__.py:
import apipkg
apipkg.initpkg(__name__, {
'path': {
'Class1': "_mypkg.somemodule:Class1",
}
})
Requires Python 3.7 or later; works on CPython, Jython, and PyPy.
Verify before relying
- Whether embedding the code directly into your project is practical given the stated ~200 lines of code.
- Performance characteristics of the lazy-loading approach in your specific use case.
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 1,060 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,551,611/month — #3,775 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: apipkg-3.0.2-py3-none-any.whl
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
lazy-importsDefers loading of module attributes and their…
permissive · top 5,000 on PyPI
donfigDonfig provides a unified configuration system…
permissive · top 5,000 on PyPI
grimpGrimp builds a queryable graph of Python…
permissive · top 5,000 on PyPI
lazy-loaderLazy-loader defers the import of subpackages…
permissive · top 1,000 on PyPI
zope.deferredimportDefers module imports until they are actually…
unclear · top 15,000 on PyPI
pytest-helpers-namespaceProvides a pytest namespace for registering and…
permissive · top 15,000 on PyPI
pippip is the standard installer for Python…
permissive · top 100 on PyPI
publicationPublication hides private implementation…
permissive · top 5,000 on PyPI
import-depsAnalyzes Python module imports statically using…
permissive · top 5,000 on PyPI
azure-nspkgProvides the namespace package infrastructure…
permissive · top 5,000 on PyPI