首页 > 解决方案 > 每当使用 import cv2 时,OpenCV 都会出错

问题描述

我在终端上使用 pip3 install opencv-contrib-python 安装了 cv2 并且它可以工作,但是在 python IDLE 上,每当我尝试导入 cv2 或运行导入了 cv2 的 vscode 文件时,它都会说

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/cv2.cpython-38-darwin.so, 2): Symbol not found: _inflateValidate
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/.dylibs/libpng16.16.dylib (which was built for Mac OS X 10.13)
  Expected in: /usr/lib/libz.1.dylib
 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cv2/.dylibs/libpng16.16.dylib

在终端。知道如何解决这个问题吗?

标签: pythonpython-3.xopencv

解决方案


I had the same question and I found it's because I use a high version of opencv (4.X.X), and my system version is low (mac os 10.12.5). So I installed a lower version of opencv (3.4.5.20), and then the question is solved.

You can use the following command to list the versions of opencv:

pip install opencv-python==

ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 3.4.8.29, 3.4.9.31, 4.0.0.21, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30, 4.2.0.32) ERROR: No matching distribution found for opencv-python==

Then you can try a lower version (3.4.5.20, for example), and install it using:

pip install opencv-python==3.4.5.20

Then you can retry import cv2 to see whether the question is solved.


推荐阅读