apipkg Tutorial: Enhanced Lazy Loading in Python

Welcome to this tutorial on using the apipkg library in Python! In this tutorial, we’ll explore how to use the apipkg library to efficiently manage your imports and only import modules when they are actually accessed. This technique is known as lazy loading or deferred importing.

There are other libraries which can accomplish similar tasks, such as importlib. But apipkg has something unique about it, that takes lazy loading to the next level. Let’s find out what!

What is apipkg?

The apipkg library is a Python package that provides a way to control the importing behavior of your modules. It allows you to define mappings between attribute names and module paths. This means that you can delay the actual import of a module until a specific attribute or function from that module is accessed.

This can lead to improved performance and reduced memory consumption in your applications. It is also possible that some modules never need to be imported (if that particular feature wasn’t needed by the user). This leads to even more performance benefits.

Most importantly, lazy loading modules reduces the startup time for your application. This is a big deal in applications where startup time is critical.


Before we begin, make sure you have the apipkg library installed. You can install it using pip:

pip install apipkg

Setting Up the Project Structure

Let’s start by creating the necessary files and folder structure for this tutorial.

├── main.py
└── package/
    ├── __init__.py
    ├── moduleA.py
    └── moduleB.py

Here’s a brief overview of the purpose of each file:

  • main.py: This is the main script where we’ll use the lazy loading technique with the apipkg library.
  • package/__init__.py: This file will initialize the mappings for lazy loading using the apipkg library.
  • package/moduleA.py: This module contains a function for addition.
  • package/moduleB.py: This module contains a function for multiplication.

The __init__.py file is essential for creating a package, not just for the purposes of using with apipkg. Without this file, we wouldn’t be able to import these modules.

Implementing Lazy Loading with apipkg in Python

Let’s go through the code step by step to understand how lazy loading works using the apipkg library.

Step 1: Importing the Required Modules

In the main.py file, start by importing the necessary modules:

import package
import time

We’re importing the package module, which will utilize the lazy loading technique, and the time module to add delays for demonstration purposes.

Step 2: Initializing Lazy Loading in __init__.py

In the package/__init__.py file, we’ll use the apipkg library to define the mappings for lazy loading:

import apipkg

apipkg.initpkg(__name__, {
    'path': {
        'add': "package.moduleA:add",
        'mul': "package.moduleB:mul"

Here, we’re specifying that the attribute add should be imported from the moduleA when accessed, and similarly, the attribute mul should be imported from the moduleB when accessed.

Note: The name “path” here is arbitrary.

Step 3: Implementing Lazy Loading Functions

In package/moduleA.py and package/moduleB.py, implement the functions for addition and multiplication, respectively:

# moduleA.py
print("Module A")
def add(num1, num2):
    return num1 + num2
# moduleB.py
print("Module B")
def mul(num1, num2):
    return num1 * num2

The print statements will help us visualize when the modules are actually imported. You will understand this better when we actually run the code.

Step 4: Using Lazy Loaded Functions

In main.py, let’s use the lazy loaded functions with some delays to observe the behavior:

print(package.path.add(2, 4))

print(package.path.mul(2, 4))

Here, we’re calling the add and mul functions from the package module. Remember that the actual import of the corresponding modules will only occur when these functions are accessed.

Running the Example

Now that we have everything set up, you can run the main.py script using your Python interpreter (or run it from an IDE)

python main.py

As you run the script, you will observe from the output, that “Module A” and “Module B” print statements from moduleA.py and moduleB.py only appear when the respective functions are called. This demonstrates the lazy loading behavior implemented using the apipkg library.

Here is the output:

Module A
Module B

If these modules had not been lazy loaded, both print statements would be located at the very beginning. However, this is not the case. “Module B” was only printed when that module was accessed.


This marks the end of the Python apipkg Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Notify of
Inline Feedbacks
View all comments