From f38ba3f534ce51ad8a1de920de3c3279db0832f2 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Wed, 20 Nov 2024 15:22:27 +0100 Subject: [PATCH 1/3] Add lazy loading of runtime module Signed-off-by: Alicja Miloszewska --- src/bindings/python/src/openvino/__init__.py | 3 ++- src/bindings/python/src/openvino/utils.py | 15 +++++++++++++++ tools/benchmark_tool/openvino/__init__.py | 3 ++- tools/ovc/openvino/__init__.py | 3 ++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/bindings/python/src/openvino/__init__.py b/src/bindings/python/src/openvino/__init__.py index 57f03f00c2eebf..7060e9c557e410 100644 --- a/src/bindings/python/src/openvino/__init__.py +++ b/src/bindings/python/src/openvino/__init__.py @@ -18,7 +18,8 @@ # # It is not compared with init files from openvino-dev package. # # # Import all public modules -from openvino import runtime as runtime +from openvino.utils import lazy_import +runtime = lazy_import('openvino.runtime') from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/src/bindings/python/src/openvino/utils.py b/src/bindings/python/src/openvino/utils.py index 9890ae9b3e6460..636232e0757f74 100644 --- a/src/bindings/python/src/openvino/utils.py +++ b/src/bindings/python/src/openvino/utils.py @@ -7,6 +7,7 @@ from functools import wraps from typing import Callable, Any from pathlib import Path +import importlib.util def _add_openvino_libs_to_search_path() -> None: @@ -113,3 +114,17 @@ def __get__(self, obj: Any, cls: Any = None) -> Any: _patch(func, deprecated(name, version, message, stacklevel)) return func return decorator + + +def lazy_import(module_name): + spec = importlib.util.find_spec(module_name) + if spec is None: + raise ImportError(f"Module {module_name} not found") + + + loader = importlib.util.LazyLoader(spec.loader) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + + loader.exec_module(module) + return module diff --git a/tools/benchmark_tool/openvino/__init__.py b/tools/benchmark_tool/openvino/__init__.py index 57f03f00c2eebf..7060e9c557e410 100644 --- a/tools/benchmark_tool/openvino/__init__.py +++ b/tools/benchmark_tool/openvino/__init__.py @@ -18,7 +18,8 @@ # # It is not compared with init files from openvino-dev package. # # # Import all public modules -from openvino import runtime as runtime +from openvino.utils import lazy_import +runtime = lazy_import('openvino.runtime') from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/tools/ovc/openvino/__init__.py b/tools/ovc/openvino/__init__.py index 57f03f00c2eebf..7060e9c557e410 100644 --- a/tools/ovc/openvino/__init__.py +++ b/tools/ovc/openvino/__init__.py @@ -18,7 +18,8 @@ # # It is not compared with init files from openvino-dev package. # # # Import all public modules -from openvino import runtime as runtime +from openvino.utils import lazy_import +runtime = lazy_import('openvino.runtime') from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental From e063d92f7e6181d6841cae5f957510581abe9ae8 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Wed, 20 Nov 2024 15:26:31 +0100 Subject: [PATCH 2/3] Add warning Signed-off-by: Alicja Miloszewska --- src/bindings/python/src/openvino/runtime/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bindings/python/src/openvino/runtime/__init__.py b/src/bindings/python/src/openvino/runtime/__init__.py index df2cab57bd0aba..485cfe5d464631 100644 --- a/src/bindings/python/src/openvino/runtime/__init__.py +++ b/src/bindings/python/src/openvino/runtime/__init__.py @@ -5,6 +5,14 @@ """openvino module namespace, exposing factory functions for all ops and other classes.""" # noqa: F401 +import warnings +warnings.warn( + "The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. " + "Please replace `openvino.runtime` with `openvino` after 25.1 release.", + DeprecationWarning, + stacklevel=2 +) + from openvino._pyopenvino import get_version __version__ = get_version() From 1bc436ee192f929f3a197981bf08481bea1ba453 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Fri, 22 Nov 2024 10:12:24 +0100 Subject: [PATCH 3/3] Give more specific warning Signed-off-by: Alicja Miloszewska --- src/bindings/python/src/openvino/__init__.py | 2 +- .../python/src/openvino/runtime/__init__.py | 25 ++++++++++++++----- src/bindings/python/src/openvino/utils.py | 6 ++--- tools/benchmark_tool/openvino/__init__.py | 2 +- tools/ovc/openvino/__init__.py | 2 +- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/bindings/python/src/openvino/__init__.py b/src/bindings/python/src/openvino/__init__.py index 7060e9c557e410..d0ad5b650b8c0c 100644 --- a/src/bindings/python/src/openvino/__init__.py +++ b/src/bindings/python/src/openvino/__init__.py @@ -19,7 +19,7 @@ # # # Import all public modules from openvino.utils import lazy_import -runtime = lazy_import('openvino.runtime') +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/src/bindings/python/src/openvino/runtime/__init__.py b/src/bindings/python/src/openvino/runtime/__init__.py index 485cfe5d464631..437df7df94ec11 100644 --- a/src/bindings/python/src/openvino/runtime/__init__.py +++ b/src/bindings/python/src/openvino/runtime/__init__.py @@ -6,12 +6,25 @@ # noqa: F401 import warnings -warnings.warn( - "The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. " - "Please replace `openvino.runtime` with `openvino` after 25.1 release.", - DeprecationWarning, - stacklevel=2 -) +warnings.simplefilter("always", DeprecationWarning) +import inspect +stack = inspect.stack() + +if stack[-1].code_context is None: # python interactive mode + warnings.warn( + "The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. " + "Please replace `openvino.runtime` with `openvino` after 25.1 release.", + DeprecationWarning, + stacklevel=2 + ) +else: + warnings.warn( + "The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. " + "Please replace `openvino.runtime` with `openvino` after 25.1 release.\n" + f"{stack[-1].filename}:{stack[-1].lineno}:\n\t{stack[-1].code_context[0]}", + DeprecationWarning, + stacklevel=2 + ) from openvino._pyopenvino import get_version diff --git a/src/bindings/python/src/openvino/utils.py b/src/bindings/python/src/openvino/utils.py index 636232e0757f74..53ade9f86c4847 100644 --- a/src/bindings/python/src/openvino/utils.py +++ b/src/bindings/python/src/openvino/utils.py @@ -8,6 +8,7 @@ from typing import Callable, Any from pathlib import Path import importlib.util +from types import ModuleType def _add_openvino_libs_to_search_path() -> None: @@ -116,12 +117,11 @@ def __get__(self, obj: Any, cls: Any = None) -> Any: return decorator -def lazy_import(module_name): +def lazy_import(module_name: str) -> ModuleType: spec = importlib.util.find_spec(module_name) - if spec is None: + if spec is None or spec.loader is None: raise ImportError(f"Module {module_name} not found") - loader = importlib.util.LazyLoader(spec.loader) module = importlib.util.module_from_spec(spec) sys.modules[module_name] = module diff --git a/tools/benchmark_tool/openvino/__init__.py b/tools/benchmark_tool/openvino/__init__.py index 7060e9c557e410..d0ad5b650b8c0c 100644 --- a/tools/benchmark_tool/openvino/__init__.py +++ b/tools/benchmark_tool/openvino/__init__.py @@ -19,7 +19,7 @@ # # # Import all public modules from openvino.utils import lazy_import -runtime = lazy_import('openvino.runtime') +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/tools/ovc/openvino/__init__.py b/tools/ovc/openvino/__init__.py index 7060e9c557e410..d0ad5b650b8c0c 100644 --- a/tools/ovc/openvino/__init__.py +++ b/tools/ovc/openvino/__init__.py @@ -19,7 +19,7 @@ # # # Import all public modules from openvino.utils import lazy_import -runtime = lazy_import('openvino.runtime') +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental