skillfed

apipkg

apipkg: namespace control and lazy-import mechanism

apipkg v3.0.2 1.6M downloads/30d#3,775 on PyPI55
Permissive license Active released

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 apipkg

uv

uv add apipkg

poetry

poetry add apipkg

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: MacOS :: MacOS XOperating System :: Microsoft :: WindowsOperating System :: POSIXProgramming Language :: PythonProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonTopic :: Software Development :: Libraries

Tags

lazy import mechanismnamespace control pythondeferred module loadingapi namespace managementreduce package importspython import optimization
lazy-loadingnamespace-control

More Libraries packages