Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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 virtually all Python versions, including CPython2.3 to Python3.1, Jython and PyPy. It co-operates 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 <100 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",
        'Class2': "_mypkg.othermodule:Class2",
    }
}

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.sub.Class1   # '_mypkg.somemodule' gets imported now
<class _mypkg.somemodule.Class1 at 0xb7d428fc>
>>> mypkg.sub.Class2   # '_mypkg.othermodule' gets imported now
<class _mypkg.somemodule.Class1 at 0xb7d428fc>

The mypkg.sub namespace and both its classes are lazy loaded. Note that no imports apart from the root ‘import mypkg’ is required. This means that whoever uses your Api only ever needs this one import. Of course you can still use the import statement like so:

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

have fun, holger krekel

Metadata

Release files for apipkg 1.0b4

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 1.0b4
File Size Uploaded
apipkg-1.0b4.tar.gz 3.4 kB Details

Release files / apipkg-1.0b4.tar.gz

Download URL apipkg-1.0b4.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4800554a4be3da7504d4140c104b7270fb7f3168a3e618f7ef19e8e473d3a856
BLAKE2b-256 checksum
How to use checksums
bcd68a7faba95f538033f32839998aa84a1ca396bdf6bef920cb4e3932bb91be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

2.1.0

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

This release

1.0b4 This release

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