Closed pwuertz closed 1 year ago
There is any news regarding this issue? I think that this problem is the same that occurs with new opencv version.
There is any news regarding this issue? I think that this problem is the same that occurs with new opencv version.
Confirmed. Latest version opencv having same problem. Before problem didn't reproduced.
I'm also facing this problem with the latest versions: PyInstaller 4.6 opencv-python 4.5.3.58
For temporary workaround, I downgraded opencv version to 4.5.3.56. and It's working fine with PyInstaller 4.6 .
Yeah, it looks like that horrible loader shim made it into wheels. I was hoping this was an oversight, but opencv/opencv-python#569 seems to suggest this was intentional (even though as far as I can tell, the library search path magic is largely redundant (especially compared to what were doing before) as wheels are statically linked against OpenCV, so the library path they are trying to set does not exist).
But even if we were to collect those config scripts, the module swapping trick module does not work with PyInstaller at the moment, presumably because our FrozenImporter
is immune to changes to sys.path
, so it always loads the collected cv2/__init__.py
wrapper (from PYZ), leading to recursion error. I suppose we'll have to modify it to give precedence to on-filesystem files (or at least binary extensions), or maybe try to put it at the end of sys.meta_path
so that on-filesystem resources are handled first using the built-in loaders/importers.
I'm also facing this problem with the latest versions: PyInstaller 4.6 opencv-python 4.5.3.58
For temporary workaround, I downgraded opencv version to 4.5.3.56. and It's working fine with PyInstaller 4.6 .
I also tried with downgrade the version but still getting same error, ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
I'm also facing this problem with the latest versions: PyInstaller 4.6 opencv-python 4.5.3.58 For temporary workaround, I downgraded opencv version to 4.5.3.56. and It's working fine with PyInstaller 4.6 .
I also tried with downgrade the version but still getting same error, ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
Downgrading only opencv-python to 4.5.3.56 worked for me.
In the OpenCV build configuration, there is an option OPENCV_SKIP_PYTHON_LOADER
. Although I'd expect this to bypass the loader as it says on the tin, enabling the debug in OpenCV still shows the loader doing its thing. That's not really a PyInstaller problem though.
I just wanted to mention that this option existed and was curious if this knowledge was of any use. I haven't dug into the cmake files thoroughly enough to know if this option can be tested against in an installed OpenCV.
EDIT: Made a fool of myself in the OpenCV GitHub page, so I'll update here too. I removed the install files from my Python environment and ran install on OpenCV again. It appears to work, though I haven't tested it thoroughly to be sure. If this does actually work, would it be possible to provide some kind of warning that OpenCV needs to be built in this way when using PyInstaller?
This is legitimately a blocker on MacOS since pip install opencv-python-headless<4.6
fails while building wheel for opencv-python-headless. While opencv-python-headless-4.6.0.66
installed correctly. And I don't know of a workaround at the moment.
Edit: See comment below
How does PyInstaller in any way inhibit wheel building? The two are completely unrelated.
This is legitimately a blocker on MacOS since
pip install opencv-python-headless<4.6
fails while building wheel for opencv-python-headless. Whileopencv-python-headless-4.6.0.66
installed correctly. And I don't know of a workaround at the moment.
4.5.4.60
provides binary wheels for python 3.10, so it installs just fine...
How does PyInstaller in any way inhibit wheel building? The two are completely unrelated.
It's a catch 22. PyInstaller has issues with OpenCV-python 4.6. The solution/workaround mentioned here and at pyinstaller/pyinstaller#6889 is to go back to 4.5 instead. But OpenCV-python 4.5 simply won't build on my MacOS machine. (no problem on Windows and Linux)
It's entirely possible there's another solution I'm not aware of. Like forking OpenCV and making a patch. But I don't curerntly have the answer so as far as I'm concerned it's impossible to use opencv with pyinstaller on a mac at the moment.
4.5.4.60 provides binary wheels for python 3.10, so it installs just fine...
numpy fails to build on 3.10. The wheels for mac were added in 1.21.4
, but opencv-python-headless-4.5.5.64
is pinned to numpy-1.21.2
opencv-python-headless>=4.5.4,<4.6
opencv-python-headless>=4.5.4,<4.6
If pip
is trying to build opencv-python-headless
from sdist (.tar.gz), you have some other problem going on (because on Windows and macOS, you probably won't have all dependencies for it).
On my test system, it installs pre-build wheels that are available on PyPI, and install without problems:
$ python3.10 -m venv venv
$ . venv/bin/activate
(venv) $ pip install "opencv-python-headless>=4.5.4,<4.6" --no-cache-dir
Collecting opencv-python-headless<4.6,>=4.5.4
Downloading opencv_python_headless-4.5.5.64-cp36-abi3-macosx_10_15_x86_64.whl (46.3 MB)
|████████████████████████████████| 46.3 MB 11.3 MB/s
Collecting numpy>=1.21.2
Downloading numpy-1.23.1-cp310-cp310-macosx_10_9_x86_64.whl (18.1 MB)
|████████████████████████████████| 18.1 MB 11.5 MB/s
Installing collected packages: numpy, opencv-python-headless
Successfully installed numpy-1.23.1 opencv-python-headless-4.5.5.64
Downloading opencv_python_headless-4.5.5.64-cp36-abi3-macosx_10_15_x86_64.whl https://pypi.org/project/opencv-python-headless/#files
Y'know what, I think the macos version I'm using might just be too old at 10.14.6
I updated to 11.0.1 and it correctly installed from wheel in seconds instead of taking minutes.
Despite the Backward compatibility
section at the bottom of the PyPi insinuating 10.13+ should be supported.
No longer an issue on my side then, and I now know why.
Those wheels are marked as >=10.15 compatible so yes, trying to install on 10.14 will lead to installation from source.
When pyinstalling an application with an OpenCV-built-from-source dependency, the bundled application fails importing
cv2
with an error message like this:This problem exists on Linux and Windows alike (also reported and discussed in https://github.com/pyinstaller/pyinstaller/issues/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:
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 toimport cv2
withinhook-cv2.py
, and then tell PyInstaller to just package the modulecv2
points to instead of trying to analyze thecv2/
module dir?