Search code examples
pythonpython-importpython-modulepython-importlib

How do I use importlib.LazyLoader?


In my module, I have a couple of functions that depend on an external module with a long startup time. How do I use LazyLoader? If I have

import veggies

or

import veggies.brussels.sprouts

or

from veggies.brussels import sprouts

how would I replace those statements to use LazyLoader such that the execution of the contents of the module are postponed until needed?

It is not immediately obvious from the documentation how to use it. There is no example, and nullege code search only comes up with the unit test included with Python itself.


Solution

  • The original issue has some code that seems to to a full import lazily:

    The following files imports two modules lazily:

    import sys
    import importlib.util
    
    def lazy(fullname):
      try:
        return sys.modules[fullname]
      except KeyError:
        spec = importlib.util.find_spec(fullname)
        module = importlib.util.module_from_spec(spec)
        loader = importlib.util.LazyLoader(spec.loader)
        # Make module with proper locking and get it inserted into sys.modules.
        loader.exec_module(module)
        return module
    
    os = lazy("os")
    myown = lazy("myown")
    
    print(os.name)
    myown.test()
    

    To test, I used the following in myown.py.

    print("Executed myown.")
    def test():
      print("OK")
    

    That worked nicely (Python 3.8a0).