Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 3.0.2 instead.
Reason given by maintainers: broken on python 3.4 but claims support

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 2.7 and 3.4+, 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 2.1.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 2.1.0
File Size Uploaded
apipkg-2.1.0.tar.gz 16.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for apipkg 2.1.0
File Interpreter ABI Platform
apipkg-2.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 23.4 kB

Release files / apipkg-2.1.0.tar.gz

Download URL apipkg-2.1.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a4be31cf8081e660d2cdea6edfb8a0f39f385866abdcfcfa45e5a0887345cb70
BLAKE2b-256 checksum
How to use checksums
4690c54454c5f87ae7c754626cdc71499f6c1b3d7cdd13a4a7f27a20e05a1ad3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / apipkg-2.1.0-py2.py3-none-any.whl

Download URL apipkg-2.1.0-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e5d85ce3a2e622734c5daebb213df044d4cedee1031fbe98add3c73ca5995b94
BLAKE2b-256 checksum
How to use checksums
cd5206d530ad5fa1329a8ce64179800e1e49688db48dead063a0e23fdce8c76f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.1

2 release files

This release

2.1.0 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

1 release file

1.0

1 release file

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