The __import__ statement is a low-level function in Python that takes a string as input and triggers the actual import operation. Low-level functions are part of the core Python language and are typically meant to be used for library development or for accessing operating system resources, and are not commonly used for application development.
We can use this keyword to import the random library in our myrandom.py module as follows:
#import random
random = __import__('random')
The rest of the code in myrandom.py can be used as it is without any change. I have illustrated a simple case of using the __import__ method for academic reasons and will skip the advanced details for those of you who are interested in exploring as further reading. The reason for this is that the __import__ method is not recommended to be used for user applications; it is designed more for interpreters.
The importlib.import_module statement is the one to be used other than the regular import for advanced functionality.
We can import any module using the importlib library. The importlib library offers a variety of functions, including __import__, related to importing modules in a more flexible way. Here is a simple example of how to import a random module in our myrandom.py module using importlib:
import importlib
random = importlib.import_module('random')
The rest of the code in myrandom.py can be used as it is without any change. The importlib module is best known for importing modules dynamically and is very useful in cases where the name of the module is not known in advance and we need to import the modules at runtime. This is a common requirement for the development of plugins and extensions.
Commonly used functions available in the importlib module are as follows:
• __import__: This is the implementation of the __import__ function, as already discussed.
• import_module: This is used to import a module and is most commonly used to load a module dynamically. In this method, you can specify whether you want to import a module using an absolute or relative path. The import_module function is a wrapper around importlib.__import__. Note that the former function brings back the package or module (for example, packageA.module1), which is specified with the function, while the latter function always returns the top-level package or module (for example, packageA).
• importlib.util.find_spec: This is a replaced method for the find_loader method, which is deprecated since Python release 3.4. This method can be used to validate whether the module exists and it is valid.
• invalidate_caches: This method can be used to invalidate the internal caches of finders stored at sys.meta_path. The internal cache is useful to load the module faster without triggering the finder methods again. But if we are dynamically importing a module, especially if it is created after the interpreter began execution, it is a best practice to call the invalidate_caches method.
This function will clear all modules or libraries from the cache to make sure the requested module is loaded from the system path by the import system.
• reload: As the name suggests, this function is used to reload a previously imported module. We need to provide the module object as an input parameter for this function. This means the import function has to be done successfully.
This function is very helpful when module source code is expected to be edited or changed and you want to load the new version without restarting the program.
0 comments:
Post a Comment