Usage of Dot "." Operator in Relative Import
In the above code, "..module4" represents that module4 is present in the parent folder to the current location.
importlib Module
Apart from the usual, importlib.import_module() and built-in __import__() can be used for importing a module or package or library. The importlib modules provide an API for interacting with the import system of Python. A direct call to __import__ performs only the module search and if the module is found, the module is compiled. The drawbacks in using the components of importing system of Python are as follows.
- __import__() performs only the module search and if the module is found, the compilation process is executed. The name binding operation is not performed while using __import__().
- Once the module is compiled, the recent changes in the modules, which are done after compiling the module will not be reflected when importing the modules.
__import__() Function
>>> help(__import__) Help on built-in function __import__ in module builtins: __import__(name, globals=None, locals=None, fromlist=(), level=0) Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to use importlib.import_module() to programmatically import a module. The globals argument is only used to determine the context; they are not modified. The locals argument is unused. The fromlist should be a list of names to emulate ``from name import ...``, or an empty list to emulate ``import name``. When importing a module from a package, note that __import__('A.B', ...) returns package A when fromlist is empty, but its submodule B when fromlist is not empty. The level argument is used to determine whether to perform absolute or relative imports: 0 is absolute, while a positive number is the number of parent directories to search relative to the current module.