Skip to main content

Welcome to apipkg !

With apipkg you can control the exported namespace of a Python package and greatly reduce the number of imports for your users. It is a small pure Python module that works on CPython 3.7+, Jython and PyPy. It cooperates well with Python’s help() system, custom importers (PEP302) and common command-line completion tools.

Usage is very simple: you can require ‘apipkg’ as a dependency or you can copy paste the ~200 lines of code into your project.

Tutorial example

Here is a simple mypkg package that specifies one namespace and exports two objects imported from different modules:

# mypkg/__init__.py
import apipkg
apipkg.initpkg(__name__, {
    'path': {
        'Class1': "_mypkg.somemodule:Class1",
        'clsattr': "_mypkg.othermodule:Class2.attr",
    }
}

The package is initialized with a dictionary as namespace.

You need to create a _mypkg package with a somemodule.py and othermodule.py containing the respective classes. The _mypkg is not special - it’s a completely regular Python package.

Namespace dictionaries contain name: value mappings where the value may be another namespace dictionary or a string specifying an import location. On accessing an namespace attribute an import will be performed:

>>> import mypkg
>>> mypkg.path
<ApiModule 'mypkg.path'>
>>> mypkg.path.Class1   # '_mypkg.somemodule' gets imported now
<class _mypkg.somemodule.Class1 at 0xb7d428fc>
>>> mypkg.path.clsattr  # '_mypkg.othermodule' gets imported now
4 # the value of _mypkg.othermodule.Class2.attr

The mypkg.path namespace and its two entries are loaded when they are accessed. This means:

  • lazy loading - only what is actually needed is ever loaded

  • only the root “mypkg” ever needs to be imported to get access to the complete functionality

  • the underlying modules are also accessible, for example:

from mypkg.sub import Class1

Including apipkg in your package

If you don’t want to add an apipkg dependency to your package you can copy the apipkg.py file somewhere to your own package, for example _mypkg/apipkg.py in the above example. You then import the initpkg function from that new place and are good to go.

Feedback?

If you have questions you are welcome to

Metadata

Release files for apipkg 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for apipkg 3.0.0
File Size Uploaded Provenance Actions
apipkg-3.0.0.tar.gz 6.6 kB Without provenance Details for apipkg-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for apipkg 3.0.0
File Interpreter ABI Platform Actions
apipkg-3.0.0-py3-none-any.whl Python 3 none any Details for apipkg-3.0.0-py3-none-any.whl

Total release size: 15.1 kB

Release files / apipkg-3.0.0.tar.gz

Download URL apipkg-3.0.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f8e504547b753fdff33d126f27b5cfbb5f67864db3ecfdd4bf34aeefb6709bf9
BLAKE2b-256 checksum
How to use checksums
3445c5b223313d173c05207f46854e0a2c866c45e2789450964eb2f4ba48f0b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / apipkg-3.0.0-py3-none-any.whl

Download URL apipkg-3.0.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0f98b9b426a0569d8dc0ca318e050319500fc546681d600414f09b8bfecb44c
BLAKE2b-256 checksum
How to use checksums
0816a3b26766e74a1fa12a960c1ab18abc82627cade188e8181c8e10c859a2e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for apipkg 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page