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 returns the imported module object if import success. If the module does not exist, then it will throw ModuleNotFoundError error. This article will show you how to use the __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 modules dynamically in the 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 functions in this module. Please see the code comments for a detailed explanation.
    # Import getmembers, isfunction function from inspect module.
    from inspect import getmembers, isfunction
    
    # This function will list all the functions and attributes of the module. 
    def list_module_attributes(mod_name):
        
        # First import the module object by module name.
        imported_module = __import__(mod_name, globals=None, locals=None, fromlist=True)
        
        # The dir() function will return all attributes and functions of the module object.
        mod_attrs_str = dir(imported_module)
        
        print(mod_attrs_str)
        
        ''' The inspect.getmembers() function will get all members of the module object.  
            The inspect.isfunction() function will check whether the module member is a function or not.
            Below code will return module functions only.
        '''
        functions_list = [o for o in getmembers(imported_module) if isfunction(o[1])]
        
        # Each element in the functions_list is a tuple object.
        for func in functions_list:
            
            # The first element of the tuple is the function name. 
            print(func[0])
    
    
    # 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()
    
        #list_module_attributes('lib.mod1')
        
        #list_module_attributes('lib.mod2')
        
        #list_module_attributes('lib.mod3')
    
  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

2. Question & Answer.

2.1 How to fix ImportError: No module named mod when import python modules dynamically.

  1. There is a ‘modules’ directory in my python app, and there are two python modules module_1.py and module_2.py in the ‘modules’ directory. And my team member may add more python module files such as module_3.py, module_4.py …… in the modules directory, so I want to dynamically import all the python modules in the python app modules directory like below.
    # First get all the module files name in the python app modules directory.
    all_module_files_array = []
    all_module_files_array = os.listdir('modules')
    
    # Set the python modules directory to the system path variable.
    sys.path.append('/Users/jerry/test_python_app/modules')
    
    # Loop in the module files array.
    for mod in all_module_files_array:
        # Import the python module by file name.
        import mod

    But when I run the above source code, it shows the ImportError: No module named mod error. How to fix this error?

  2. You can not use python import instruction to import string variables. If you do want to import all the python module files dynamically in the source code, you can use the python importlib library’s import_module method like below.
    # First import the python importlib module.
    import importlib
    
    # Get all the module files name in the python app modules folder.
    all_module_files_array = [] 
    all_module_files_array = os.listdir('modules')
    
    ...
    
    # Loop in the module files array.
    for mod in all_module_files_array:
    
        # Import the python mod with importlib.import_module() method.
        importlib.import_module(mod)

Subscribe to receive more programming tricks.

We don’t spam!

Subscribe to receive more programming tricks.

We don’t spam!

Leave a Comment

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.