python - Import submodule functions into the parent namespace -
i have package of commonly used utility functions import tons of different projects, using different parts. i'm following patterns i've seen in numpy
, doing things like:
utils/ __init__.py ``` . import math . import plot ``` math/ __init__.py ``` . import core . core import * . import stats . stats import * __all__ = [] __all__.extend(core.__all__) __all__.extend(stats.__all__) ``` core.py ``` __all__ = ['cfunc1', 'cfunc2'] def cfunc1()... def cfunc2()... ``` stats.py ``` __all__ = ['sfunc1', 'sfunc2'] def sfunc1()... def sfunc2()... ``` plot/ __init__.py
the nice thing structure can call submodule functions higher level namespaces, e.g. utils.math.cfunc1()
, utils.math.sfunc2()
. issue is: both core
, stats
take long time import, don't want automatically import them both math.__init__
.
is there way not import both stats
, core
default, , instead use import utils.math.core
, still have functions in core
end in math
namespace? i.e. able do:
import utils.math.core utils.math.cfunc1()
Comments
Post a Comment