Share
Embed
Transcript
8 slides
-
Import hooks in Python Rohit Sanjay Junior Backend Engineer at
Noteable Twitter: @imrohitsanj GitHub: @rohitsanj BangPypers Lightning Talk - May 2022 bit.ly/import-hooks -
How does importing work in Python? Python first tries to
look for your module in sys.modules which is a dict that maps module names to modules which have already been loaded. -
How does importing work in Python? - If the named
module is not found in sys.modules, then Python’s import protocol is invoked to find and load the module. - This protocol consists of two conceptual objects, finders and loaders. - A finder’s job is to determine whether it can find the named module using whatever strategy it knows about. - Loader’s job is to load the module (execute the code in the module and register the globals) - Objects that implement both of these interfaces are referred to as importers. -
How does importing work in Python? - Finder objects need
to implement a method called find_spec which returns a ModuleSpec. This ModuleSpec object is used to encapsulate all import-related information (what the name is and which loader instance to use to load it) - Loader objects need to implement a method called exec_module which just fetches and executes the code in the module and binds it to the namespace of the module name. - If you want to make the same object as a finder and loader, you will need to define another method called create_module. -
How does importing work in Python? These are the default
importer objects present in sys.meta_path -
Let’s build our own importer Live demo of building a
DB importer that imports code from the database. https://github.com/rohitsanj/import-hook-python -
Use cases - Deployment pipeline with database commits - Block/deprecated
list of modules - Either stop user from importing certain modules or let them know that the module being imported is deprecated and must not be used -
References - Hooking into the import system - Fred Phillips
- The import system - Python Language Reference