Search code examples
pythonlazy-loadingimport

Lazy loading module imports in an __init__.py file python


I was wondering if anyone had any suggestions for lazy loading imports in an init file? I currently have the following folder structure:

/mypackage
    __init__.py
    /core
        __init__.py
        mymodule.py
        mymodule2.py

The init.py file in the core folder with the following imports:

from mymodule import MyModule
from mymodule2 import MyModule2

This way I can just do:

from mypackage.core import MyModule, MyModule2

However, in the package init.py file, I have another import:

from core.exc import MyModuleException

This has the effect that whenever I import my package in python, MyModule and MyModule2 get imported by default because the core init.py file has already been run.

What I want to do, is only import these modules when the following code is run and not before:

from mypackage.core import MyModule, MyModule2

Any ideas?

Many thanks.


Solution

  • You can't. Remember that when python imports it executes the code in the module. The module itself doesn't know how it is imported hence it cannot know whether it has to import MyModule(2) or not.

    You have to choose: allow from mypackage.core import A, B and from core.exc import E does the non-needed imports (x)or do not import A and B in core/__init__.py, hence not allowing from mypackage.core import A, B.

    Note: Personally I would not import MyModule(2) in core/__init__.py, but I'd add an all.py module that does this, so the user can do from mypackage.core.all import A, B and still have from mypackage.core.exc import TheException not loading the unnecessary classes.

    (Actually: the all module could even modify mypackage.core and add the classes to it, so that following imports of the kind from mypackage.core import MyModule, MyModule2 work, but I think this would be quite obscure and should be avoided).