OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[TF FE] Stop get_environment_setup from raising NameError on Python 3.13 (#39039)
Daksha8244402f2e36
diff --git a/src/bindings/python/src/openvino/frontend/tensorflow/utils.py b/src/bindings/python/src/openvino/frontend/tensorflow/utils.pyindex a46c5556cc6..99ef1faffaa 100644--- a/src/bindings/python/src/openvino/frontend/tensorflow/utils.py+++ b/src/bindings/python/src/openvino/frontend/tensorflow/utils.py@@ -5,6 +5,7 @@# mypy: ignore-errors+import importlibimport logging as logimport numpy as npimport sys@@ -88,9 +89,7 @@ def get_environment_setup(framework):env_setup['python_version'] = python_versiontry:if framework == 'tf':- exec("import tensorflow")- env_setup['tensorflow'] = get_imported_module_version(sys.modules["tensorflow"])- exec("del tensorflow")+ env_setup['tensorflow'] = get_imported_module_version(importlib.import_module("tensorflow"))except (AttributeError, ImportError):passenv_setup['sys_platform'] = sys.platformdiff --git a/tools/ovc/openvino/tools/ovc/environment_setup_utils.py b/tools/ovc/openvino/tools/ovc/environment_setup_utils.pyindex 38d486285fc..1e160edda4b 100644--- a/tools/ovc/openvino/tools/ovc/environment_setup_utils.py+++ b/tools/ovc/openvino/tools/ovc/environment_setup_utils.py@@ -1,6 +1,7 @@# Copyright (C) 2018-2026 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import importlibimport osimport sys@@ -41,9 +42,7 @@ def get_environment_setup(framework):env_setup['python_version'] = python_versiontry:if framework == 'tf':- exec("import tensorflow")- env_setup['tensorflow'] = get_imported_module_version(sys.modules["tensorflow"])- exec("del tensorflow")+ env_setup['tensorflow'] = get_imported_module_version(importlib.import_module("tensorflow"))except (AttributeError, ImportError):passenv_setup['sys_platform'] = sys.platformdiff --git a/tools/ovc/unit_tests/ovc/utils/environment_setup_test.py b/tools/ovc/unit_tests/ovc/utils/environment_setup_test.pynew file mode 100644index 00000000000..98d72b2215a--- /dev/null+++ b/tools/ovc/unit_tests/ovc/utils/environment_setup_test.py@@ -0,0 +1,35 @@+# Copyright (C) 2018-2026 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import sys+import types++import pytest++from openvino.frontend.tensorflow.utils import get_environment_setup as fe_get_environment_setup+from openvino.tools.ovc.environment_setup_utils import get_environment_setup as ovc_get_environment_setup+++def fake_tensorflow(monkeypatch):+ # Stands in for an installed TensorFlow: only the version attribute is read.+ module = types.ModuleType("tensorflow")+ module.__version__ = "2.20.0"+ monkeypatch.setitem(sys.modules, "tensorflow", module)+ return module++[email protected]("get_environment_setup", [fe_get_environment_setup, ovc_get_environment_setup])+def test_environment_setup_reports_tensorflow_version(get_environment_setup, fake_tensorflow):+ env_setup = get_environment_setup("tf")+ assert env_setup["tensorflow"] == "2.20.0"+ assert env_setup["sys_platform"] == sys.platform++[email protected]("get_environment_setup", [fe_get_environment_setup, ovc_get_environment_setup])+def test_environment_setup_without_tensorflow(get_environment_setup, monkeypatch):+ # a None entry in sys.modules makes the import raise ImportError+ monkeypatch.setitem(sys.modules, "tensorflow", None)+ env_setup = get_environment_setup("tf")+ assert "tensorflow" not in env_setup+ assert env_setup["sys_platform"] == sys.platform