结合PyInstaller使用OpenCV

1l5u6lss  于 2023-03-09  发布在  其他
关注(0)|答案(4)|浏览(166)

当我尝试用PyInstaller创建Python程序的二进制可执行文件时,我收到一个错误。然而,www.example.com文件似乎有一些问题。如果我尝试导入cv2,我收到以下错误消息: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

我使用下面的代码:

import cv2

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

这在我用Python运行的时候完全没问题。
为了编译可执行文件,我运行:

pyinstaller --onefile local-processing/exec_test.py

当PyInstaller运行时,它有以下输出:

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.

我正在macOS High Sierra上使用Python 3.7.2、OpenCV 4.0.1和PyInstaller 3.4,以及virtualenv 16.3.0版本,如果有任何其他信息需要帮助,请告诉我。
谢谢!

bweufnob

bweufnob1#

确保您的OpenCV和Pyinstaller版本是最新的:

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

您也可以尝试全新安装。

ca1c2owp

ca1c2owp2#

在python中获取cv2的路径,并在编译时添加:

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
r3i60tvu

r3i60tvu3#

不久前我也遇到过同样的问题。要在Windows上解决这个问题,方法非常相似。不同之处在于,你必须确保找到pyinstaller加载信息的正确文件夹,因为一旦你有了cv 2容器之外的其他示例,这就很棘手了。例如,在我的机器上,只有当我传递路径为--path=AppData\Local\pyinstaller\bincache00_py39_64bit\cv2时,我才能解决这个问题。在pyinstaller命令的末尾添加该标志,一切都应该正常工作。

dauxcl2d

dauxcl2d4#

您还可以尝试包括

"--collect-all opencv-python"

原始代码,然后变为:

pyinstaller --onefile --collect-all opencv-python local-processing/exec_test.py

相关问题