Fix Python bindings when optional modules are disabled#28660
Merged
asmorkalov merged 1 commit intoopencv:4.xfrom Mar 16, 2026
Merged
Fix Python bindings when optional modules are disabled#28660asmorkalov merged 1 commit intoopencv:4.xfrom
asmorkalov merged 1 commit intoopencv:4.xfrom
Conversation
Fix two issues that cause `import cv2` to fail when building with -DBUILD_opencv_gapi=OFF: 1. In `has_all_required_modules()`, the function parameter `type_node` was ignored in favor of the enclosing scope's loop variable `node`. This works by accident when called as `has_all_required_modules(node)` but is incorrect — the parameter should be used directly. 2. In `__load_extra_py_code_for_module()`, only `ImportError` was caught. When a stale gapi submodule directory exists from a previous build, gapi/__init__.py raises `AttributeError` (not `ImportError`) because it tries to access C++ bindings that don't exist. Now catches both exception types for defense-in-depth. Refs: opencv#26098
4 tasks
VadimLevin
approved these changes
Mar 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two issues that cause
import cv2to fail when building OpenCV with-DBUILD_opencv_gapi=OFF -DWITH_GAPI=OFF.PR #26633 added
required_modulesfiltering to the typing stubs generator, which addressed the main problem. However, two issues remain:1. Wrong variable in
has_all_required_modules()(generation.py)The function parameter is named
type_nodebut the body usesnode— the loop variable from the enclosing scope:This works by accident because the function is only called as
has_all_required_modules(node), so the closure variablenodehappens to equal the argument. Fixed by usingtype_node.required_modules.2.
__load_extra_py_code_for_module()only catchesImportError(cv2/__init__.py)When a stale
gapi/submodule directory persists from a previous build (e.g., in incremental builds), the runtime loader discovers it and attempts to import it. Thegapi/__init__.pycallscv.gapi_GNetPackage(...)at import time, which raisesAttributeErrorwhen the gapi C++ bindings are absent. The loader only catchesImportError, so theAttributeErrorpropagates and crashesimport cv2.Fixed by catching
(ImportError, AttributeError).Files Changed
modules/python/src2/typing_stubs_generation/generation.py— use function parameter instead of closure variablemodules/python/package/cv2/__init__.py— catchAttributeErrorin addition toImportErrorTesting
Verified with OpenCV 4.11.0 built with
-DBUILD_opencv_gapi=OFF -DWITH_GAPI=OFF:import cv2succeeds without manual post-install cleanupResolves #26098