Recently, I made a Python package that lets you import JSON files directly. Let’s say we have some JSON files in the below file structure. Working w

MetaPathFinders! or how to change python import behavior

submited by
Style Pass
2021-06-11 14:30:02

Recently, I made a Python package that lets you import JSON files directly. Let’s say we have some JSON files in the below file structure.

Working with JSON files is not a big deal for Python programmers and the purpose of writing this package is indeed educational. It helps me to understand the import mechanism of the Python language and now I want to tell you that.

A MetaPathFinder is a class that can be derived and tell Python where and how it can find and load a module.

At first, we should know about sys.meta_path , according to Python documentation:

A list of meta path finder objects that have their find_spec() methods called to see if one of the objects can find the module to be imported.

In other words, sys.meta_path is a list of objects that Python asks them to see if they can find the module or not, respectively. Each of these objects must have a method called find_spec that should return a ModuleSpec instance if and only if they can find the module and returns None otherwise.

Leave a Comment