How we can bundle multiple python modules?

Working with complex programs that span thousands of lines creates two fundamental problems: managing code to locate specific functionality and debugging errors efficiently. Python solves this by allowing you to break code into multiple modules and bundle them into a single executable script.

Python provides functionality to bundle different modules into a single executable, enabling easier management and debugging of complex programs.

Method 1: Using ZIP Files

You can bundle multiple Python modules by creating a ZIP file and executing it directly. The key requirement is having a main.py file as the entry point.

Syntax

python file_name.zip

Example

First, create the main.py module that imports and uses functions from other modules ?

# main.py
from sum_fibonacci import fibonacci

output = fibonacci(10)
print(output)

Next, create the sum_fibonacci.py module with the fibonacci function ?

# sum_fibonacci.py
def fibonacci(term):
    first_val = 0
    second_val = 1
    sum_fibonacci = 0
    for num in range(0, term):
        sum_fibonacci = sum_fibonacci + first_val
        next_val = first_val + second_val
        first_val = second_val
        second_val = next_val
    return sum_fibonacci

After creating both files, zip them into fibonacci.zip and execute ?

python fibonacci.zip

The output shows the sum of the first 10 Fibonacci numbers ?

88

Method 2: Using Stickytape

Stickytape is a tool that automatically bundles multiple Python modules into a single script file. Install it using pip install stickytape.

Syntax

stickytape main.py > output_file_name.py

Example

Create the main.py module that imports from other modules ?

# main.py
from calculate import Calculation
from fact_find import factorial

# Create calculation object with value 5
calc = Calculation(value=5)

# Calculate factorial using the value
output = factorial(calc.value)
print(output)

Define the Calculation class in calculate.py ?

# calculate.py
class Calculation:
    def __init__(self, value):
        self.value = value  # Store the input value

Create the factorial function in fact_find.py ?

# fact_find.py
def factorial(fact):
    if fact == 1:
        return fact
    else:
        return fact * factorial(fact - 1)  # Recursive calculation

Use stickytape to bundle all modules into a single file ?

stickytape main.py > calculate_factorial.py

The bundled file can now be executed normally ?

python calculate_factorial.py

The output shows the factorial of 5 ?

120

Comparison

Method Installation Required File Type Best For
ZIP Files No .zip Built-in Python solution
Stickytape Yes (pip install) .py Creating standalone scripts

Conclusion

Both ZIP files and Stickytape effectively bundle multiple Python modules. Use ZIP files for a built-in solution, or Stickytape when you need a single Python script file for distribution.

---
Updated on: 2026-03-24T17:03:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements