Using OpenCV with PyInstaller

Question:

I am getting an error when trying to create a binary executable of a Python program with PyInstaller. However, there seems to be some issue with the config.py file. If I try to import cv2, I get the following error message:

Traceback (most recent call last):
File "exec_test.py", line 1, in <module>
File "/Users/me/path/to/.env/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/cv2/__init__.py", line 89, in <module>
File "site-packages/cv2/__init__.py", line 58, in bootstrap
File "site-packages/cv2/__init__.py", line 56, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. 
Check OpenCV installation.
[10914] Failed to execute script exec_test

I am using the following code:

import cv2

if __name__ == '__main__':
    print("Hello, world!")

which is totally fine when I run it with Python.

To compile the executable, I am running:

pyinstaller --onefile local-processing/exec_test.py

When the PyInstaller runs, it has the following output:

50 INFO: PyInstaller: 3.4
50 INFO: Python: 3.7.2
55 INFO: Platform: Darwin-17.7.0-x86_64-i386-64bit
56 INFO: wrote /Users/me/path/to/neural-network/exec_test.spec
59 INFO: UPX is not available.
60 INFO: Extending PYTHONPATH with paths
['/Users/me/path/to/neural-network/local-processing',
 '/Users/me/path/to/neural-network']
60 INFO: checking Analysis
66 INFO: Building because /Users/me/path/to/neural-network/local-processing/exec_test.py changed
66 INFO: Initializing module dependency graph...
68 INFO: Initializing module graph hooks...
70 INFO: Analyzing base_library.zip ...
2885 INFO: running Analysis Analysis-00.toc
2891 INFO: Caching module hooks...
2894 INFO: Analyzing /Users/me/path/to/neural-network/local-processing/exec_test.py
3366 INFO: Processing pre-find module path hook   distutils
3366 INFO: distutils: retargeting to non-venv dir '/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/distutils/__init__.py'
4023 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
4409 INFO: Processing pre-find module path hook   site
4410 INFO: site: retargeting to fake-dir '/Users/me/path/to/.env/lib/python3.7/site-packages/PyInstaller/fake-modules'
6439 INFO: Loading module hooks...
6439 INFO: Loading module hook "hook-pkg_resources.py"...
6921 INFO: Processing pre-safe import module hook   win32com
6923 INFO: Loading module hook "hook-scipy.py"...
6923 INFO: Loading module hook "hook-encodings.py"...
6977 INFO: Loading module hook "hook-setuptools.py"...
7237 WARNING: Hidden import "distutils.command.build_ext" not found!
7467 INFO: Loading module hook "hook-cv2.py"...
7468 INFO: Loading module hook "hook-sysconfig.py"...
7474 INFO: Loading module hook "hook-numpy.core.py"...
7536 INFO: Loading module hook "hook-xml.py"...
7590 INFO: Loading module hook "hook-pydoc.py"...
7590 INFO: Loading module hook "hook-numpy.py"...
7618 INFO: Looking for ctypes DLLs
7651 INFO: Analyzing run-time hooks ...
7655 INFO: Including run-time hook 'pyi_rth_pkgres.py'
7656 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
7666 INFO: Looking for dynamic libraries
7802 INFO: Looking for eggs
7802 INFO: Using Python library /Users/me/path/to/.env/bin/../.Python
7809 INFO: Warnings written to /Users/me/path/to/neural-network/build/exec_test/warn-exec_test.txt
7856 INFO: Graph cross-reference written to /Users/me/path/to/neural-network/build/exec_test/xref-exec_test.html
7875 INFO: checking PYZ
7878 INFO: Building because toc changed
7878 INFO: Building PYZ (ZlibArchive) /Users/me/path/to/neural-network/build/exec_test/PYZ-00.pyz
8535 INFO: Building PYZ (ZlibArchive) /Users/me/path/to/neural-network/build/exec_test/PYZ-00.pyz completed successfully.
8549 INFO: checking PKG
8550 INFO: Building because toc changed
8550 INFO: Building PKG (CArchive) PKG-00.pkg
15351 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
15355 INFO: Bootloader /Users/me/path/to/.env/lib/python3.7/site-packages/PyInstaller/bootloader/Darwin-64bit/run
15355 INFO: checking EXE
15356 INFO: Building because toc changed
15356 INFO: Building EXE from EXE-00.toc
15357 INFO: Appending archive to EXE /Users/me/path/to/neural-network/dist/exec_test
15368 INFO: Fixing EXE for code signing /Users/me/path/to/neural-network/dist/exec_test
15372 INFO: Building EXE from EXE-00.toc completed successfully.

I am using Python 3.7.2, OpenCV version 4.0.1 and PyInstaller version 3.4 with a virtualenv version 16.3.0 on a macOS High Sierra. Please let me know if any other information would be helpful.

Thanks!

Asked By: student1868

||

Answers:

Ensure your OpenCV and Pyinstaller versions are up to date with:

pip install --upgrade opencv-python
pip install --upgrade pyinstaller

You can always try a fresh install as well.

Answered By: nathancy

Get the path of the cv2 in python and add while compiling :

import cv2 
print(cv2.file) # /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.so
    
/home:~$ pyinstaller --onefile --paths="/usr/local/lib/python3.6/dist-packages/cv2/python-3.6" main.py

I was having the same problem not long ago.
To solve that issue on Windows, it’s pretty similar. The difference is that you have to make sure that you found the proper folder where pyinstaller is loading info from, because it’s tricky once you have other instances other than the cv2 container. In my machine, for example, I could only solve it when I passed the path as –path=AppDataLocalpyinstallerbincache00_py39_64bitcv2 Add that flag at the end of your pyinstaller command and everything should work just fine.

Answered By: Faoli

You could also try to include

"--collect-all opencv-python" 

to your original code, which then becomes:

pyinstaller --onefile --collect-all opencv-python local-processing/exec_test.py
Answered By: Fabian Kamp