Skip to content

Commit 6507959

Browse files
committed
gh-148588: Clarify __lazy_modules__ with relative import example
1 parent b6e14eb commit 6507959

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Doc/reference/simple_stmts.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,17 @@ Python versions older than 3.15 while leveraging lazy imports on 3.15+::
948948
import pathlib # loaded lazily
949949

950950
Relative imports are resolved to their absolute name before the lookup, so
951-
the sequence must always contain fully qualified module names::
951+
the sequence must always contain fully qualified module names. For
952+
``from``-style imports, the name to list is the module being imported
953+
*from* (the base module, left of the ``import`` keyword), not the names
954+
imported from it::
952955

953956
# In mypackage/mymodule.py
954-
__lazy_modules__ = ["mypackage"]
957+
__lazy_modules__ = ["mypackage", "mypackage.sub.utils"]
955958

956-
from . import utils # loaded lazily: resolves to mypackage
957-
import json # loaded eagerly (not in __lazy_modules__)
959+
from . import helper # loaded lazily: . resolves to mypackage
960+
from .sub.utils import func # loaded lazily: .sub.utils resolves to mypackage.sub.utils
961+
import json # loaded eagerly (not in __lazy_modules__)
958962

959963
Imports inside functions, class bodies, or
960964
:keyword:`try`/:keyword:`except`/:keyword:`finally` blocks are always eager,

0 commit comments

Comments
 (0)