1111import os
1212import sys
1313import types
14+ import warnings
1415
1516__all__ = ["attach" , "load" , "attach_stub" ]
1617
@@ -121,34 +122,45 @@ def load(fullname, error_on_import=False):
121122 We often see the following pattern::
122123
123124 def myfunc():
124- from numpy import linalg as la
125- la .norm(...)
125+ import numpy as np
126+ np .norm(...)
126127 ....
127128
128- This is to prevent a module, in this case `numpy`, from being
129- imported at function definition time, since that can be slow.
129+ Putting the import inside the function prevents, in this case,
130+ `numpy`, from being imported at function definition time.
131+ That saves time if `myfunc` ends up not being called.
130132
131- This function provides a proxy module that, upon access, imports
133+ This `load` function returns a proxy module that, upon access, imports
132134 the actual module. So the idiom equivalent to the above example is::
133135
134- la = lazy.load("numpy.linalg ")
136+ np = lazy.load("numpy")
135137
136138 def myfunc():
137- la .norm(...)
139+ np .norm(...)
138140 ....
139141
140142 The initial import time is fast because the actual import is delayed
141143 until the first attribute is requested. The overall import time may
142144 decrease as well for users that don't make use of large portions
143- of the library.
145+ of your library.
146+
147+ Warning
148+ -------
149+
150+ While lazily loading subpackages technically works, it causes the
151+ package (that contains the subpackage) to be eagerly loaded even
152+ if the package is already lazily loaded.
153+ So, you probably shouldn't use subpackages with this `load` feature.
154+ Instead you should encourage the package maintainers to use the
155+ lazy_loader `attach` feature to make their subpackages lazily load.
144156
145157 Parameters
146158 ----------
147159 fullname : str
148160 The full name of the module or submodule to import. For example::
149161
150162 sp = lazy.load('scipy') # import scipy as sp
151- spla = lazy.load('scipy.linalg') # import scipy.linalg as spla
163+
152164 error_on_import : bool
153165 Whether to postpone raising import errors until the module is accessed.
154166 If set to `True`, import errors are raised as soon as `load` is called.
@@ -165,6 +177,14 @@ def myfunc():
165177 except KeyError :
166178 pass
167179
180+ if "." in fullname :
181+ msg = (
182+ "subpackages can technically be lazily loaded, but it causes the "
183+ "package to be eagerly loaded even if it is already lazily loaded."
184+ "So, you probably shouldn't use subpackages with this lazy feature."
185+ )
186+ warnings .warn (msg , RuntimeWarning )
187+
168188 spec = importlib .util .find_spec (fullname )
169189 if spec is None :
170190 if error_on_import :
0 commit comments