Importing programmatically
To programmatically import a module, use importlib.import_module().
Checking if a module can be imported
If you need to find out if a module can be imported without actually doing the import, then you should use importlib.util.find_spec().
Note that if name is a submodule (contains a dot), importlib.util.find_spec() will import the parent module.
Importing a source file directly
To import a Python source file directly, use the following recipe:
Implementing lazy imports
The example below shows how to implement lazy imports:
Got an article suggestion?
Let us know
BY
NC
SA
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.