Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - Using OpenCV with PyInstaller

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...