- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
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.
Following is the syntax of executing zip file in python −
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.
The output gives the sum of first 10 numbers in the Fibonacci series.
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.
Following is the syntax to create single file using stickytape −
stickytape main.py > output_file_name.py
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
The output is the factorial of 5.
- Related Articles
- How we can import Python modules in Jython?
- How we can import Python modules without installing?
- Can we keep Python modules in compiled format?
- How we can copy Python modules from one system to another?
- Can we iteratively import python modules inside a for loop?
- How to use multiple modules with Python import Statement?
- How can we print multiple blank lines in python?
- How we can extend multiple Python classes in inheritance?
- How we can split Python class into multiple files?
- How can we do Python operator overloading with multiple operands?
- How to use multiple resource bundle in same JSP?
- How do we use easy_install to install Python modules?
- How can we combine multiple print statements per line in Python?
- How we can break a string with multiple delimiters in Python?
- How can we display all modules with classloaders in Java 9?