@@ -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,56 @@ 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+ .. _lazy :
877+
878+ Lazy imports
879+ ------------
880+
881+ .. index ::
882+ pair: lazy; import
883+ single: lazy import
884+
885+ The :keyword: `lazy ` keyword marks an import as lazy. It is a :ref: `soft keyword
886+ <soft-keywords>` that only has special meaning when it appears immediately
887+ before an :keyword: `import ` or :keyword: `from ` statement.
888+
889+ When an import statement is preceded by the :keyword: `lazy ` keyword,
890+ the import becomes *lazy *: the module is not loaded immediately at the import
891+ statement. Instead, a lazy proxy object is created and bound to the name. The
892+ actual module is loaded on first use of that name.
893+
894+ Lazy imports are only permitted at module scope. Using ``lazy `` inside a
895+ function, class body, or :keyword: `try `/:keyword: `except `/:keyword: `finally `
896+ block raises a :exc: `SyntaxError `. Star imports cannot be lazy (``lazy from
897+ module import * `` is a syntax error), and :ref: `future statements <future >`
898+ cannot be lazy.
899+
900+ When using ``lazy from ... import ``, each imported name is bound to a lazy
901+ proxy object. The first access to any of these names triggers loading of the
902+ entire module and resolves only that specific name to its actual value. Other
903+ names remain as lazy proxies until they are accessed.
904+
905+ Example::
906+
907+ lazy import json
908+
909+ print('json' in sys.modules) # False - module not loaded yet
910+
911+ # First use triggers loading
912+ result = json.dumps({"hello": "world"})
913+
914+ print('json' in sys.modules) # True - now loaded
915+
916+ If an error occurs during module loading (such as :exc: `ImportError ` or
917+ :exc: `SyntaxError `), it is raised at the point where the lazy import is first
918+ used, not at the import statement itself.
919+
920+ See :pep: `810 ` for the full specification of lazy imports.
921+
922+ .. versionadded :: next
923+
873924.. _future :
874925
875926Future statements
0 commit comments