@@ -748,14 +748,15 @@ The :keyword:`!import` statement
748748 pair: name; binding
749749 pair: keyword; from
750750 pair: keyword; as
751+ pair: keyword; lazy
751752 pair: exception; ImportError
752753 single: , (comma); import statement
753754
754755.. productionlist :: python-grammar
755- import_stmt: "import" `module ` ["as" `identifier `] ("," `module ` ["as" `identifier `])*
756- : | "from" `relative_module ` "import" `identifier ` ["as" `identifier `]
756+ import_stmt: ["lazy"] "import" `module ` ["as" `identifier `] ("," `module ` ["as" `identifier `])*
757+ : | ["lazy"] "from" `relative_module ` "import" `identifier ` ["as" `identifier `]
757758 : ("," `identifier ` ["as" `identifier `])*
758- : | "from" `relative_module ` "import" "(" `identifier ` ["as" `identifier `]
759+ : | ["lazy"] "from" `relative_module ` "import" "(" `identifier ` ["as" `identifier `]
759760 : ("," `identifier ` ["as" `identifier `])* [","] ")"
760761 : | "from" `relative_module ` "import" "*"
761762 module: (`identifier ` ".")* `identifier `
@@ -870,6 +871,57 @@ determine dynamically the modules to be loaded.
870871
871872.. audit-event :: import module,filename,sys.path,sys.meta_path,sys.path_hooks import
872873
874+
875+ .. _lazy-imports :
876+
877+ Lazy imports
878+ ------------
879+
880+ .. index ::
881+ pair: lazy; import
882+ single: lazy import
883+
884+ When an import statement is preceded by the :keyword: `lazy<lazy import> ` keyword,
885+ the import becomes *lazy *: the module is not loaded immediately at the import
886+ statement. Instead, a lazy proxy object is created and bound to the name. The
887+ actual module is loaded on first use of that name.
888+
889+ .. keyword :: lazy import
890+
891+ The ``lazy `` keyword marks an import as lazy. It is a :ref: `soft keyword
892+ <soft-keywords>` that only has special meaning when it appears immediately
893+ before an :keyword: `import ` or :keyword: `from ` statement.
894+
895+ Lazy imports are only permitted at module scope. Using ``lazy `` inside a
896+ function, class body, or :keyword: `try `/:keyword: `except `/:keyword: `finally `
897+ block raises a :exc: `SyntaxError `. Star imports cannot be lazy (``lazy from
898+ module import * `` is a syntax error), and :ref: `future statements <future >`
899+ cannot be lazy.
900+
901+ When using ``lazy from ... import ``, each imported name is bound to a lazy
902+ proxy object. The first access to any of these names triggers loading of the
903+ entire module and resolves only that specific name to its actual value. Other
904+ names remain as lazy proxies until they are accessed.
905+
906+ Example::
907+
908+ lazy import json
909+
910+ print('json' in sys.modules) # False - module not loaded yet
911+
912+ # First use triggers loading
913+ result = json.dumps({"hello": "world"})
914+
915+ print('json' in sys.modules) # True - now loaded
916+
917+ If an error occurs during module loading (such as :exc: `ImportError ` or
918+ :exc: `SyntaxError `), it is raised at the point where the lazy import is first
919+ used, not at the import statement itself.
920+
921+ See :pep: `810 ` for the full specification of lazy imports.
922+
923+ .. versionadded :: next
924+
873925.. _future :
874926
875927Future statements
0 commit comments