Lazy loading module imports in an __init__.py file python

我怕爱的太早我们不能终老 提交于 2019-12-08 08:17:38

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).

Unless I'm mistaking your intentions, this is actually possible but requires some magic.

Basically, subclass types.ModuleType and override __getattr__ to import on demand.

Check out the Werkzeug init.py for an example.

Not sure if it applies here but in general lazy loading of modules can be done using the Importing package.

Works like this:

from peak.util.imports import lazyModule
my_module = lazyModule('my_module')

Now my module is only really imported when you use it the first time.

If your modules structure is like:

/mypackage
  __init__.py
  /core
    __init__.py
    MyModule.py
    MyModule2.py

or:

/mypackage
  __init__.py
  /core
    __init__.py
    /MyModule
      __init__.py
    /MyModule2
      __init__.py

then feel free to use

from mypackage.core import MyModule, MyModule2

without importing them in __init__.py under mypackage/core

You may use follow code in __init__ in module:

import apipkg
apipkg.initpkg(__name__, {
    'org': {
        'Class1': "secure._mypkg:Class1",
        'Class2': "secure._mypkg2:Class2",
    }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!