How it Works¶
PEP 302 defines a protocol for managing module imports. The protocol boils down to this:
There are two components:
FindersandLoaders.Findersare responsible for, unsurprisingly, finding modules.If a
Finderlocates a module, i.e. knows which loader can load a module, it returns aModuleSpec.This
ModuleSpeccontains information on the module, such as filename and package, and identifies whichLoadercan execute the load.The
Loaderis, as you’ve guessed it, responsible for loading modules into the environment.It does so by first creating a module object, which the python machinery places into the
sys.modulesdictionary, then compiling and executing the module code.An object that can both Find and Load is called an
Importer.
inline-importer works by placing the source code of modules in a dictionary, keyed by module name.
The finder searches this dictionary for an entry whose key matches the given module name.
If found, it returns a ModuleSpec with itself listed as the Loader.
Then, when python calles the Loader, inline-importer simply compiles the inlined source code to python bytecode and executes it as the normal python loader would.