How To Use __import__() Function to Load / Import Python Package, Module Dynamically

Python, known for its dynamic nature, provides various mechanisms to dynamically load modules during runtime. One such method is the __import__() function. This powerful function enables developers to import modules dynamically, allowing for flexible and adaptable code structures. This article will show you how to use the __import__() function with examples.

1. Understanding the __import__() Function.

  1. The __import__() function in Python is a built-in function that allows the import of a module by name during the execution of a program.
  2. It is particularly useful when the name of the module to be imported is not known until runtime.
  3. The function can be used to import both built-in and external modules.

1.1 Syntax.

  1. Below is the Python __import__() function syntax.
    __import__(name, globals=None, locals=None, fromlist=(), level=0)
  2. Parameters:
  3. name (str): The name of the module to import, the name is in string format.
  4. globals (dict, optional): A dictionary representing the current global symbol table. Defaults to the current globals.
  5. locals (dict, optional): A dictionary representing the current local symbol table. Defaults to the current locals.
  6. fromlist (tuple, optional): Specifies the objects that should be imported from the module. Defaults to an empty tuple.
  7. level (int, optional): Indicates whether to use absolute or relative imports. Defaults to 0.

1.2 Return Value.

  1. The __import__() function returns the imported module object if import success.
  2. If the module does not exist, then it will throw ModuleNotFoundError error. 

2. Examples of Using __import__().

  1. Example 1: Basic Usage.
    mod = __import__('math')
    print(mod.sqrt(16))  # Output: 4.0
    
  2. Example 2: Importing from Submodules.
    mod = __import__('os.path',fromlist=('path'))
    print(mod.abspath('test.txt')) # Output: D:\WorkSpace\Work\python-courses\test.txt
  3. Example 3: Importing from Packages.
    def test_import_urllib_request():
        mod = __import__('urllib.request', fromlist=('request'))
        print(mod.urlopen('https://www.baidu.com'))  # Output: <http.client.HTTPResponse object at 0x0000023D5EA106D0>
    
    if __name__ == "__main__":
    
        test_import_urllib_request()
  4. Example 4: Dynamically Importing Based on User Input.
    def test_import_by_user_input():
        module_name = input("Enter the module name to import: ")
        try:
            mod = __import__(module_name)
            print("Module", module_name, "imported successfully.")
        except ImportError:
            print("Module", module_name, "not found.")
    
    if __name__ == "__main__":
        test_import_by_user_input()
    
    '''
    Enter the module name to import: os.path
    Module os.path imported successfully.
    '''

3. Load / Import Custom 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

4. Question & Answer.

4.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)

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.