Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Import hooks in Python - BangPypers Lightning Talk - May 2022

Import hooks in Python - BangPypers Lightning Talk - May 2022

Rohit Sanjay

May 21, 2022
Tweet

More Decks by Rohit Sanjay

Other Decks in Programming

Transcript

  1. Import hooks in Python Rohit Sanjay Junior Backend Engineer at

    Noteable Twitter: @imrohitsanj GitHub: @rohitsanj BangPypers Lightning Talk - May 2022 bit.ly/import-hooks
  2. 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.
  3. 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.
  4. 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.
  5. How does importing work in Python? These are the default

    importer objects present in sys.meta_path
  6. 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
  7. 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
  8. References - Hooking into the import system - Fred Phillips

    - The import system - Python Language Reference