How To Load / Import Python Package, Module Dynamically

Python provide built-in function __import__(name[, globals[, locals[, fromlist[, level]]]]) for us to load / import python module in python source code dynamically. The name parameter is the module name in string format. The fromlist parameter is a boolean value. When fromlist is True, it means the module name can contain package name ( for example: lib.mod1), when fromlist is False, it means the module name should not contain the module package ( for example: mod1 ).

The __import__() function return the imported module object if import success. If the module do not exist, then it will throw ModuleNotFoundError error. This article will show you how to use __import__() function with examples.

1. Load / Import Python Module Dynamically Example Steps.

  1. Create a PyDev project in eclipse ( How To Run Python In Eclipse With PyDev ).
  2. Create below package and modules, there are 3 modules mod1, mod2, mod3 in com.dev2qa.example.import.lib package. We will import the three module dynamically in TestImportFunction module.
    python dynamically import modules example
  3. There are 2 methods in each lib module ( mod1, mod2, mod3 ).
  4. mod1.py

    def method_1():
        print("mod1.method_1()")
    
    def method_2():
        print("mod1.method_2()")
  5. mod2.py

    def method_3():
        print("mod1.method_3()")
    
    def method_4():
        print("mod1.method_4()")
  6. mod3.py

    def method_5():
        print("mod1.method_5()")
    
    def method_6():
        print("mod1.method_6()")
  7. TestImportFuncton.py. There are 2 function in this module. Please see code comments for detail explanation.
    '''
    Created on Feb 11, 2020
    
    @author: songzhao
    '''
    
    # This function let you input multiple module name and import them one by one.
    def import_multiple_modules():
        
        # Input module names separated by white space.
        inp = input("Input multiple module-name separated with white space:  ").strip()
        
        # Split module names.
        args = inp.split(" ")
        
        # Loop in the module names list.
        for mod_name in args:
            try:
                ''' Import module by it's name, the fromlist = True make the module name can contain package name. 
                    The return value is the imported module object.
                '''
                imported_module = __import__(mod_name, globals=None, locals=None, fromlist=True)
                
                # Print the imported moudle name.
                print(imported_module.__name__)
            # If the module do not exist, then throw ModuleNotFound error.    
            except ModuleNotFoundError as e:
                print(e)
     
               
    # This function let you import one module and run the specified module function. 
    def import_one_module():
        # Let user input module name and function name. 
        inp = input("Input module-name function-name separated with white space:  ").strip()
        
        # Split the argument to get module-name and function-name.
        args = inp.split(" ")
        
        if(len(args) == 2):
            
            # Get module name and function name.
            mod_name = args[0]
            func_name = args[1]
            
            # Import the module by name, and return the module object. fromlist = True make the module name can contain package name.
            imported_module = __import__(mod_name, globals=None, locals=None, fromlist=True)
            #imported_module = __import__(mod_name, globals=None, locals=None, fromlist=False)
            
            # Print imported module name.
            print("Imported module name is ", imported_module.__name__)
            
            # If the module contain the input function.
            if(hasattr(imported_module, func_name)):
                
                # Get the function attribute.
                func = getattr(imported_module,func_name)
                
                # Run the function.
                func()
            else:
                print("Module ", mod_name, " do not has function ", func_name)
            
        else:
            print("Input arguments count error.")
    
    if __name__ == '__main__':
        
        import_one_module()
        
        #import_multiple_modules()
  8. Below is the output when run TestImportFuncton.import_one_module() function.

    # When input correct module name and method name.
    >>>Input module-name function-name separated with white space:  lib.mod1 method_1
    Imported module name is  lib.mod1
    mod1.method_1()
    ------------------------------------------------------------------------
    
    # When input correct module name and wrong method name
    >>>Input module-name function-name separated with white space:  lib.mod1 method
    Imported module name is  lib.mod1
    Module  lib.mod1  do not has function  method
    ------------------------------------------------------------------------
    
    # When input wrong module name.
    >>>Input module-name function-name separated with white space:  lib.mod method_1
    Traceback (most recent call last):
      File "/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/import/TestImportFuncton.py", line 67, in <module>
        import_one_module()
      File "/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/import/TestImportFuncton.py", line 45, in import_one_module
        imported_module = __import__(mod_name, globals=None, locals=None, fromlist=True)
    ModuleNotFoundError: No module named 'lib.mod'
    
  9. Below is the output when run TestImportFuncton.import_multiple_modules() function.
    >>>Input multiple module-name separated with white space:  lib.mod1 lib.mod lib.mod2
    lib.mod1
    No module named 'lib.mod'
    lib.mod2
READ :   How To Install Python Interpreter In PyCharm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.