Skip to content

cv2 import error when using OpenCV built from source #110

@pwuertz

Description

@pwuertz

When pyinstalling an application with an OpenCV-built-from-source dependency, the bundled application fails importing cv2 with an error message like this:

ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

This problem exists on Linux and Windows alike (also reported and discussed in pyinstaller/pyinstaller#4564).

I think the issue is specific to OpenCV built and installed from source, which installs some sort of dynamic loader for importing the real cv2 so/pyd module. Workarounds presented so far circumvent this problem by adding the real module path as pythonpath with high priority, thus bypassing the loader.

The python module layout created by OpenCV source builds looks like this:

./dist-packages
./dist-packages/cv2
./dist-packages/cv2/python-3.9
./dist-packages/cv2/python-3.9/cv2.cpython-39-x86_64-linux-gnu.so
./dist-packages/cv2/__init__.py
./dist-packages/cv2/__pycache__
./dist-packages/cv2/__pycache__/__init__.cpython-39.pyc
./dist-packages/cv2/__pycache__/load_config_py3.cpython-39.pyc
./dist-packages/cv2/load_config_py2.py
./dist-packages/cv2/config.py
./dist-packages/cv2/load_config_py3.py
./dist-packages/cv2/config-3.9.py

And here is the top level import script doing all sorts of library path manipulation before selecting and loading the real module dynamically: init.py

Is there something that can be done at hook-cv2.py to mitigate this problem? PyInstaller already provides all the magic for resolving and bundling .dll/.so dependencies, so in general it should be safe to skip the OpenCV loader altogether as shown by the workarounds. Is there a way to import cv2 within hook-cv2.py, and then tell PyInstaller to just package the module cv2 points to instead of trying to analyze the cv2/ module dir?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions