How we can bundle multiple python modules?


Working with complex programs that run for thousands of lines produces two fundamental problems arise – managing the code to know which functionality is implemented where and debugging the program in case of an error. Fortunately, both problems can be solved by breaking the code into multiple modules and then creating a single script file from the different modules.

Python provides this exact functionality where different python modules can be bundled into a single executable script. This functionality allows for easy management and debugging of complex programs.

Using zip

One way of bundling multiple modules in python is by creating a zip file from the different modules and then executing the zip file in python terminal. To use this method first step is to define the different modules making sure that one module is named main.py, which allows the compiler to know where the program execution will start.

The modules should then be zipped into a single file which can then be executed to get the output.

Syntax

Following is the syntax of executing zip file in python −

python file_name.zip

Example

In this example the first step is to define main.py module which imports fibonacci function from sum_fibonacci module and passes value 10 to the function.

from sum_fibonacci import fibonacci output = fibonacci(10) print(output)

The fibonacci function defined in sum_fibonacci module is used to calculate the sum of first 10 numbers in the fibonacci series and return the value to the main module.

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

The main module and sum_fibonacci module are then zipped into fibonacci.zip, which can then be executed using the code below to get the desired output.

python fibonacci.zip

Output

The output gives the sum of first 10 numbers in the Fibonacci series.

88

Using stickytape

Another way of bundling multiple modules in python is by using stickytape command. To use stickytape, first step is to define the different modules that need to be bundled, in which one module should be named main.py, since execution starts from this module.

Once all the modules are defined, then stickytape can be used on main module in python shell to automatically create a single script file that can be executed to get the output of the code.

Syntax

Following is the syntax to create single file using stickytape −

stickytape main.py > output_file_name.py

Example

The first step is to define main.py module from where the execution of program starts. The main module imports members of the other modules and is used for calling those members.

# Importing the members of the modules from calculate import Calculation from fact_find import factorial # Calling method of Calculation class in calculate module calc = Calculation(value=5) # Calling the factorial function in fact_find module output = factorial(calc.value) print(output)

Calculation class in calculate.py module is defined next to receive the value from main module and assign it to self.value object.

class Calculation: def __init__(self, value): self.value = value # assigning 5 as value

Factorial function in fact_find module is then defined to recursively calculate the factorial of 5.

factorial(fact): if fact == 1: return fact else: return fact*factorial(fact-1) # recursive function to find factorial

Finally, stickytape is used to bundle all the modules into calculate_factorial.py which can then be run to get the output of the program.

# Creating a single file which bundles all the modules stickytape main.py > calculate_factorial.py

Output

The output is the factorial of 5.

120

Updated on: 23-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements