Skip to content

OpenCV Python Bindings Import Failure Due to Invalid Syntax in typing/init.py #27995

@rgaufman

Description

@rgaufman

System Information

  • OS: Ubuntu 22.04.5 LTS (Jammy)
  • Python Version: 3.10.12
  • GCC Version: 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04.2)
  • Architecture: x86_64

OpenCV Build Details

Detailed description

Problem Description

After compiling OpenCV from the latest git master (as of 2025-11-07), the Python bindings fail to import with a syntax error in the typing stub file.

Error Output

  $ python3 -c "import cv2; print(cv2.__version__)"
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 181, in <module>
      bootstrap()
    File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 175, in bootstrap
      if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
    File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
      py_module = importlib.import_module(module_name)
    File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "/usr/local/lib/python3.10/dist-packages/cv2/typing/__init__.py", line 135
      cv::Vec2d = _typing.Sequence[float]
         ^
  SyntaxError: invalid syntax

Expected Behavior

OpenCV should import successfully without syntax errors.

Actual Behavior

Import fails immediately with a SyntaxError due to C++ namespace syntax (cv::) appearing in a Python file.

Steps to reproduce

CMake Configuration

cmake \
  -DCMAKE_BUILD_TYPE=RELEASE \
  -DCMAKE_INSTALL_PREFIX=/usr/local \
  -DCMAKE_C_FLAGS="-O2 -mtune=generic -ffunction-sections -fdata-sections" \
  -DCMAKE_CXX_FLAGS="-O2 -mtune=generic -ffunction-sections -fdata-sections -Wno-deprecated" \
  -DCMAKE_EXE_LINKER_FLAGS="-Wl,--gc-sections" \
  -DCMAKE_SHARED_LINKER_FLAGS="-Wl,--gc-sections" \
  -DENABLE_FAST_MATH=ON \
  -DWITH_OPENMP=ON \
  -DWITH_FFMPEG=ON \
  -DWITH_OPENCL=ON \
  -DWITH_CUDA=ON \
  -DWITH_TBB=ON \
  -DWITH_V4L=ON \
  -DBUILD_opencv_java=OFF \
  -DBUILD_TESTS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_DOCS=OFF \
  -DBUILD_opencv_apps=OFF \
  -DWITH_QT=OFF \
  -DWITH_GTK=OFF \
  -DWITH_IPP=ON \
  -DCPU_BASELINE_DETECT=ON \
  -DCPU_BASELINE=SSE3 \
  -DCPU_DISPATCH=SSE4_1,SSE4_2,AVX,AVX2,AVX512_SKX,FP16 \
  ..

Steps to Reproduce

  1. Clone OpenCV from git master (commit from around 2025-11-07)
  2. Configure with CMake using the flags above
  3. Build with make -j$(nproc)
  4. Install with make install
  5. Attempt to import: python3 -c "import cv2"

Edit: Manually editing /usr/local/lib/python3.10/dist-packages/cv2/typing/init.py to rename cv::Vec2d to Vec2d works around the problem.

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions