Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opencv (opencv-python) package no longer works with py2exe #147

Open
urishab opened this issue Aug 30, 2022 · 5 comments
Open

opencv (opencv-python) package no longer works with py2exe #147

urishab opened this issue Aug 30, 2022 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@urishab
Copy link
Contributor

urishab commented Aug 30, 2022

opencv-python is a package that has precompiled binaries for opencv for python.
Since version 4.5.4.58 it is no longer compatible with py2exe.
With version 4.5.4.58 py2exe fails while bundling the application with the following:


  File "C:\Projects\FluHema\pyenv\lib\site-packages\py2exe\vendor\modulefinder.py", line 220, in import_hook
    self.ensure_fromlist(m, fromlist)

  File "C:\Projects\FluHema\pyenv\lib\site-packages\py2exe\vendor\modulefinder.py", line 310, in ensure_fromlist
    self.ensure_fromlist(m, all, 1)

  File "C:\Projects\FluHema\pyenv\lib\site-packages\py2exe\vendor\modulefinder.py", line 315, in ensure_fromlist
    raise ImportError("No module named " + subname)

ImportError: No module named cv2.config-3.9

With the most recent version of opencv-python, py2exe succeeds in bundling the app but the app later fails during runtime failing to find "config.py" when importing cv2

@albertosottile
Copy link
Member

Hello, thanks for reporting this. Could you provide here a minimal script and setup.py file that I can use to test this issue?

@urishab
Copy link
Contributor Author

urishab commented Aug 30, 2022

python 3.9.13
pip install py2exe opencv-python
pip freeze:

cachetools==5.2.0
future==0.18.2
numpy==1.23.2
opencv-python==4.6.0.66
pefile==2022.5.30
py2exe==0.11.1.1

test.py:

import cv2
print('hello')

py2exe_test.py:

from distutils.core import setup
import py2exe #@UnusedImport
setup(console = [{  "script": "test.py", }, ],)

run:
python py2exe_test.py py2exe
dist\test.exe

result:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "cv2\__init__.pyc", line 181, in <module>
  File "cv2\__init__.pyc", line 111, in bootstrap
  File "cv2\__init__.pyc", line 109, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

@albertosottile albertosottile added bug Something isn't working and removed requires user input labels Sep 5, 2022
@UlisesRN01
Copy link

Same bug with

Python 3.10

py2exe 0.12.0.1
opencv-python 4.6.0.66

Any solution?

@whizbuzzer
Copy link

@UlisesRN01 uninstall opencv-python and opencv-contrib-python and the install version 4.5.3.56 for both. (python -m pip install opencv-python==4.5.3.56 and python -m pip install opencv-contrib-python==4.5.3.56). Version 4.6.0.66 has a recursion related import error ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.)

@albertosottile
Copy link
Member

I gave a look at this issue and unfortunately I do not have good news. It is quite easy for me to add the config.py and config-3.py files in the bundle's archive, but then opencv-python wants to have direct filesystem access to them. See https://github.com/opencv/opencv/blob/b77330bafc497ddf65074783c0e3fb989604b555/modules/python/package/cv2/__init__.py#L99-L109 .

Essentially, opencv is now incompatible with zipimport, a key requirement to function with py2exe. This leaves us two options:

  • implement a hack on the py2exe side, similar to what was done for matplotlib here that bypasses the initialization mechanism of opencv and provides an alternative compatible with zipimport (I guess cv2.bootstrap would have to be completely reimplemented)
  • report this issue upstream, so that they can provide an alternative mechanism to load/pass these configuration files (perhaps one already exists, but I am not aware of it)

Unfortunately, I do not have the capacity for pursuing the first option (I did this for matplotlib as that package is definitely more popular, and the solution was indeed not that complicated). Therefore, I would encourage you to ask for help upstream, for the moment. In any case, I am of course willing to review PRs regarding this issue.

@albertosottile albertosottile added the help wanted Extra attention is needed label Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants