
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dynamically Add Python Modules to a Package While Programming
What are Python Modules?
Python module is a ".py" file that contains Python definitions, functions, classes, variables, constants, or any other objects. Contents in this file can be re-used in any other program.
Packaging in Python
Python packaging involves compressing bundles of Python code in a particular format that can be publically shared and can be installed by a tool like pip.
Importing Modules
To use the functionality present in any module, you need to use the import keyword along with the module name. This keyword will import the module to your current program. Syntax for this keyword is -
import module_name
Usually, modules are imported in the starting of the file which is called static imports. This usually improves the clarity and organization of the code, but not very efficient in accommodating scenarios where modules need to be added or modified at run time.
Dynamic Module Addition
The concept of dynamic module addition is creating modules while the program is executing, which is mostly used in applications that require run-time modifications such as plugins or hot-swapping components.
Below are the techniques for dynamic module addition -
Using importlib Module
The importlib includes functions that implement Python's import system for loading code from modules and packages. This module allows for more control over the import process. Below is the syntax of the importlib.import_module() function which dynamically imports a module based on its name as string -
import importlib my_module = importlib.import_module('my_module')
Modifying sys.modules
Python's sys module provides functions and variables used to manipulate different parts of the Python runtime environment. Hence, you can use the sys.modules to manipulate the dictionary to replace existing modules or add new once. The code below illustrates -
import sys import my_module sys.modules['my_module'] = my_updated_module
Using the type module
The other way is to create modules on the fly i.e., you can define new modules in memory using the types module -
import types new_module = types.ModuleType('new_module') new_module.some_function = lambda: "I am a dynamically created function!" sys.modules['new_module'] = new_module