python - 将 OpenCV 与 PyInstaller 一起使用
问题描述
尝试使用 PyInstaller 创建 Python 程序的二进制可执行文件时出现错误。但是,config.py 文件似乎存在一些问题。如果我尝试导入 cv2,我会收到以下错误消息:
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 版。请让我知道是否有任何其他信息会有所帮助。
谢谢!
解决方案
确保您的 OpenCV 和 Pyinstaller 版本是最新的:
pip install --upgrade opencv-python
pip install --upgrade pyinstaller
您也可以随时尝试全新安装。
推荐阅读
- javascript - 适配一个iframe的高度让滚动条不出现或者直接修改滚动条的美观度
- javascript - Captivate HTML5 Module Youtube 视频播放器状态
- reactjs - 使用 React PWA 清除手机浏览器上的缓存时出现错误 404
- python - tf.keras.Model 子类实现和等效的 tf 功能 API 实现在速度上是否存在差异?
- c++ - C ++中的Euler 8项目,我不知道出了什么问题
- python - 将新列添加到具有基于多个条件的值的数据框中
- javascript - Vuetify/Vue2 v-on 使 div 在悬停和单击时突出显示,我该如何阻止呢?
- php - php echo 干扰 echo json_encode
- vba - 我想添加一个名为序列号的列并填写该列直到 n 其中 n 是相邻列中的项目数
- ruby-on-rails - 如何在不存储照片的情况下上传照片以生成 PDF