Monday, June 28, 2021

What is importlib in python?

The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.

Two, the components to implement import are exposed in this package, making it easier for users to create their own custom objects (known generically as an importer) to participate in the import process.



For relative imports you have to:

  • a) use relative name
  • b) provide anchor explicitly


importlib.import_module('.c', 'a.b')


Of course, you could also just do absolute import instead:



References

https://stackoverflow.com/questions/10675054/how-to-import-a-module-in-python-with-importlib-import-module

No comments:

Post a Comment