Slide 1

Slide 1 text

Import hooks in Python Rohit Sanjay Junior Backend Engineer at Noteable Twitter: @imrohitsanj GitHub: @rohitsanj BangPypers Lightning Talk - May 2022 bit.ly/import-hooks

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

How does importing work in Python? These are the default importer objects present in sys.meta_path

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

References - Hooking into the import system - Fred Phillips - The import system - Python Language Reference