Mac 上 OpenCV/import cv2 的问题,尝试了很多东西,现在出现新错误
Posted
技术标签:
【中文标题】Mac 上 OpenCV/import cv2 的问题,尝试了很多东西,现在出现新错误【英文标题】:Troubles with OpenCV/import cv2 on Mac, have tried so many things, now new error 【发布时间】:2020-05-05 23:34:12 【问题描述】:错误:
pip3 安装 opencv-contrib-python 导入 cv2 原始错误:找不到符号:___addtf3 当前错误:找不到符号:__cg_jpeg_resync_to_restart环境:
opencv-contrib-python 4.1.2.30 iMac、macOS Catalina 10.15.2 Python 3.7.5 pip --version: pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7) 使用自制软件到目前为止我尝试过的大约 30 件事情:
Error 1:
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
Try 1:
import numpy.core.multiarray before import cv2
Error 2:
File "/Users/mbennett/Library/Python/3.7/lib/python/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/Users/mbennett/Library/Python/3.7/lib/python/site-packages/numpy/core/__init__.py", line 47, in <module>
raise ImportError(msg)
...
Original error was:
dlopen(/Users/mbennett/Library/Python/3.7/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-37m-darwin.so, 2): Symbol not found: ___addtf3
Referenced from: /Users/mbennett/Library/Python/3.7/lib/python/site-packages/numpy/core/../.dylibs/libquadmath.0.dylib
Expected in: /usr/lib/libSystem.B.dylib
in /Users/mbennett/Library/Python/3.7/lib/python/site-packages/numpy/core/../.dylibs/libquadmath.0.dylib
...
Importing the numpy c-extensions failed.
- x Try uninstalling and reinstalling numpy.
- If you have already done that, then:
x 1. Check that you expected to use Python3.7 from "/usr/local/opt/python/bin/python3.7",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.17.4" you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
--
Trying to change symlinks
Python links in 2 places: /usr/bin AND /usr/local/bin
In /usr/bin, AS ROOT, cannot remove or change python link
rm python
rm: python: Operation not permitted
# wtf, whoami -> root
--
Next try: Change order of path, put /usr/local/bin BEFORE /usr/bin, then try changing symlinks there
or try changing symlink first
(links changed, stopped here)
--
Try 1b: installing again now that pip is really pip3
No
# pip now = pip3
pip uninstall opencv-contrib-python
pip install opencv-contrib-python
OK: Successfully installed opencv-contrib-python-4.1.2.30
No, same errors
Try 1c:
No
pip uninstall opencv-contrib-python
pip install -U opencv-contrib-python
Try 1d:
No, same version numpy-1.18.1
pip uninstall numpy
pip install numpy -U
Try 1e:
No
python -c "import numpy;print numpy.version;print numpy.file";
^-- and added ()'s to print
Try 1f:
No
# put /usr/local/lib before /usr/lib
export DYLD_LIBRARY_PATH="/usr/local/lib:$DYLD_LIBRARY_PATH"
Try 1g:
No
pip install --upgrade --force-reinstall --no-cache-dir numpy
Try 1h:
No
pip install --upgrade --force-reinstall --no-cache-dir scipy
Successfully uninstalled numpy-1.18.1
Found existing installation: scipy 1.3.3
Uninstalling scipy-1.3.3:
Successfully uninstalled scipy-1.3.3
Successfully installed numpy-1.18.1 scipy-1.4.1
Try 1i:
No
Try very old scipy
# conda uninstall scipy
# conda install scipy=0.15.0
pip uninstall scipy
X pip install scipy=0.15.0 # bad syntax
x pip install scipy==0.15.0
^-- LOTS of errors
Reintalling w default version it says:
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/site-packages (from scipy) (1.18.1)
Installing collected packages: scipy
Successfully installed scipy-1.4.1
Try 1j:
No
Try rolling back scipy to 1.13.3
pip uninstall scipy
pip install scipy==1.13.3
^-- not a valid version...
Try 1k:
No
Try rolling back scipy to 1.4.0
(still uninstalled)
pip install scipy==1.4.0
Try 1l:
No
Try uninstalling the Homebrew version of numpy
# pip uninstall scipy
# brew update
# brew upgrade
brew uninstall numpy
Uninstalling /usr/local/Cellar/numpy/1.11.2... (474 files, 9.5MB)
Try 1m:
No (likely scipy is subset of numpy, vs other way around?)
Try uninstalling the Homebrew version of scipy
brew uninstall scipy
Error: No such keg: /usr/local/Cellar/scipy
Try 1n:
? Try installing blas?
? "___addtf3 which is a GCC software floating point routine provided by a gfortran"
^-- "libquadmath uses the ___addtf3 routine, rather than defines it"
Try installing gfortran
x pip install gfortran
brew install gfortran
Error:
Error: No available formula with the name "gfortran"
GNU Fortran is part of the GCC formula:
brew install gcc
Try 1o:
Install gcc:
brew install gcc
Error:
Error: gcc 6.2.0 is already installed
To upgrade to 9.2.0_3, run `brew upgrade gcc`.
,Then retry installing gfortran
, brew install gfortran
Try 1p:
No
brew upgrade gcc
brew install gfortran
Error: No available formula with the name "gfortran"
GNU Fortran is part of the GCC formula:
brew install gcc
# retest import
Try 1q:
No
Try installing clang4 or clang4-r
x brew install clang4
x brew install clang4-r
x brew install clang
--
X install-clang4.sh
--------------------
# Download binary
curl -O http://r.research.att.com/libs/clang-4.0.0-darwin15.6-Release.tar.gz
# Extract binary onto root directory
tar fvxz clang-4.0.0-darwin15.6-Release.tar.gz -C /
# Overwrite the ~/.R/Makevars
cat <<- EOF > ~/.R/Makevars
# The following statements are required to use the clang4 binary
CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++
CXX11=$CXX
CXX14=$CXX
CXX17=$CXX
CXX1X=$CXX
LDFLAGS=-L/usr/local/clang4/lib
# End clang4 inclusion statements
EOF
--
Try 1r:
x try the Sierra version of gfortran?
gfortran is part of GCC now, and Web says it's current
--
Try 1s:
No
Try overwriting python3 (links?)
cd /usr/local/Cellar/python
cp -r 3.7.5 3.7.5.bak
brew link --overwrite python3
Warning: Already linked: /usr/local/Cellar/python/3.7.5
To relink:
brew unlink python
brew link python
brew link --overwrite python
Try 1t:
X gfortran ... The missing symbol in libgcc isn't surprising
since you're trying to run code compiled with a 4.6 gfortran in a 4.2 runtime environment.
No, gfortran part of gcc, remember!
Try 1u:
No, same import error
try installing openblas
x pip install openblas
brew install openblas # ok
Try 1v:
No:
Try installing or building gdal2-python
brew install gdal2-python
Error: No formulae found in taps.
x brew reinstall --build-from-source gdal2-python
Try 1w:
No
Try fixing gcc?
cd /usr/local/lib
x chown -R mbennett gcc (already done)
brew link gcc
Warning: Already linked: /usr/local/Cellar/gcc/9.2.0_3
To relink:
brew unlink gcc # worked
brew link gcc # worked
But same error in python
Try 1x:
No, and now have different error
try installing older version of opencv 4x -> 3x
x pip install opencv-contrib-python==3.3.0.10
ERROR: Could not find a version that satisfies the requirement opencv-contrib-python==3.3.0.10 (from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.8.29, 4.0.0.21, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30)
ERROR: No matching distribution found for opencv-contrib-python==3.3.0.10
pip list | grep opencv
opencv-contrib-python 4.1.2.30
pip install opencv-contrib-python==3.4.2.16
Different error:
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
# try latest version 3
pip install opencv-contrib-python==3.4.8.29
Different Error:
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Library not loaded: @loader_path/libpng16.16.dylib
Referenced from: /usr/local/lib/python3.7/site-packages/cv2/.dylibs/libfreetype.6.dylib
Reason: Incompatible library version: libfreetype.6.dylib requires version 54.0.0 or later, but libpng16.16.dylib provides version 43.0.0
# try first version of 4x
pip install opencv-contrib-python==4.0.0.21
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
# ok, back to latest
pip install opencv-contrib-python==4.1.2.30
import cv2
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Library not loaded: @loader_path/libpng16.16.dylib
Referenced from: /usr/local/lib/python3.7/site-packages/cv2/.dylibs/libfreetype.6.dylib
Reason: Incompatible library version: libfreetype.6.dylib requires version 54.0.0 or later, but libpng16.16.dylib provides version 43.0.0
Try 1y:
try upgrading libpng
pip uninstall libpng
pip uninstall libpng
WARNING: Skipping libpng as it is not installed.
pip install libpng
ERROR: Could not find a version that satisfies the requirement libpng (from versions: none)
ERROR: No matching distribution found for libpng
X brew update libpng
brew upgrade libpng # ok
python / import cv2
New Error:
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Library not loaded: @loader_path/libtiff.5.dylib
Referenced from: /usr/local/lib/python3.7/site-packages/cv2/.dylibs/liblept.5.dylib
Reason: Incompatible library version: liblept.5.dylib requires version 11.0.0 or later, but libTIFF.dylib provides version 8.0.0
Try 1z:
try upgrading libtiff
brew upgrade libtiff # ok
import cv2 -> New Error:
File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
--
Try 1aa:
No
Try to fix Symbol not found: __cg_jpeg_resync_to_restart
cd /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources
sudo mv libJPEG.dylib libJPEG.dylib.bak
Error: mv: rename libJPEG.dylib to libJPEG.dylib.bak: Operation not permitted
sudo ln -sf libJPEG.dylib /usr/local/lib/libJPEG.dylib
sudo mv libPng.dylib libPng.dylib.bak
sudo ln -sf libPng.dylib /usr/local/lib/libPng.dylib
sudo mv libTIFF.dylib libTIFF.dylib.bak
sudo ln -sf libTIFF.dylib /usr/local/lib/libTIFF.dylib
sudo mv libGIF.dylib libGIF.dylib.bak
sudo ln -sf libGIF.dylib /usr/local/lib/libGIF.dylib
Post on Stack Overflow
【问题讨论】:
你安装了 numpy 吗? opencv 需要 numpy 才能运行。 pip install numpy @yapws87 是的,SciPy 和 NumPy 的导入给出与 cv2 相同的错误 如果我尝试使用 Anaconda3,我遇到了类似的问题和相同的错误,即使 numpy 有时也无法导入。 OpenCV 从不导入。它甚至报告相同的符号。 Google/Stack 上的许多答案都是 2016 年等。如果 Python3 的任何发行版都能在我的 Mac 上与 OpenCV 一起使用,我会很高兴。奇怪的是,在 Raspberry Pi 上运行良好,只是速度很慢。 你试过在pipenv
这样的虚拟环境中安装opencv吗?
您为什么要将libJPEG.dylib
移出系统框架?那肯定会把事情搞砸。 libJPEG.dylib
甚至存在于/usr/local/lib/
中吗?你能发布otool -l /usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so
的输出吗?
【参考方案1】:
我同意 cmets,只需使用虚拟环境,不要干扰系统的 python。但我建议 miniconda (原因:它很容易卸载,而不是 anaconda 的大小,不错的文档)。 [编辑:我编辑了对我有用的更具可重复性的步骤,并解决了 cmets 中的一些问题。]
-
卸载 Anaconda (https://docs.anaconda.com/anaconda/install/uninstall/) 和 (https://docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html#uninstalling-anaconda-or-miniconda),包括可选的第三步(查找以
# >>> conda init >>>
开头的块)
按照此处的安装步骤 (https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html)。当要求运行 conda init 时说不(我们将首先配置)。安装完成后,关闭并重新打开终端。
配置 (https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/index.html)。我的:
$ conda config --set auto_activate_base False
$ conda config --set allow_softlinks False
$ conda config --set show_channel_urls True
$ conda config --add channels conda-forge
[在原始答案中我推荐conda config --set pip_interop_enabled True
。现在我认为这是错误的建议。所以,把它排除在外(默认为 False)。]
现在发出这些命令(我假设是 catalina 的 zshell)
$ source <your-path-to-miniconda3>/bin/activate
(base) $ conda update conda
(base) $ conda init zsh
并关闭并重新启动您的 shell 以使更改生效。
注意:1) 仅将基本环境用于conda update conda
2) 在创建环境时指定 python 版本 3) 安装 python 时,您总是会得到 pip、wheels、setuptools 4) conda deactivate
5) 关于auto_activate_base
,如果你设置为 False(默认为 True),请记住 conda python 路径仅在环境激活后设置。
创建并激活一个环境:
conda create -n my-env python=3.7
conda activate my-env
甚至(对于来自 conda-forge 的 opencv 4.2 假设上述配置):
conda create -n my-opencv-env python=3.7 opencv=4.2
conda activate my-opencv-env
另外注意:如果你想在环境中安装包,记得先激活环境,然后conda install package-name
。
关于您在下面的 cmets 中提到的 numpy 问题,请先检查在创建环境时要安装的包中是否包含 numpy。然后,运行一个文件,将其导入为 np 和 print(np.__file__)
。 (你不应该在你的 miniconda 环境之外有引用)。
更多信息:https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html。您可以使用 conda 或 pip 安装软件包(我更喜欢 conda)。
【讨论】:
哇!今晚我才能检查,但谢谢!我也很欣赏关于使用 *conda 发行版的明确建议,这是我的想法。 好的,看起来很有希望。它似乎确实将 OpenCV 添加到我的系统中。但是,import cv2
从File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module> (newline) from .cv2 import *
提供ImportError: numpy.core.multiarray failed to import
,我将暂时保留它,看看您是否有其他建议。我仍然怀疑我的 ~/miniconda3 区域中的某些内容引用了 /usr/local/lib 的 python 中的任何内容。 which python
显示/Users/mbennett/opt/miniconda3/envs/my-opencv-env/bin/python
我使用了conda create -n my-opencv-env python=3.7 opencv=4.2
然后“激活”,等等。
它不应该引用您的 /usr/local...检查:1. 您的 .zshrc(如果您在安装过程中允许 conda init,您会看到它已添加到路径中) 2. (也许shell缓存了路径)重新启动你的shell,然后conda激活,然后尝试创建环境。希望它有效。
路径有点迷茫...应该是哪个顺序? .../miniconda3/bin 然后是 /usr/local/bin,然后是 /usr/bin ???感谢您的耐心等待,这是我一个多星期以来最远的一次。以上是关于Mac 上 OpenCV/import cv2 的问题,尝试了很多东西,现在出现新错误的主要内容,如果未能解决你的问题,请参考以下文章
cv2 python上的Mac照片亭,为啥我的代码会导致错误?
ImportError: dlopen(/Users/Desktop/myapp/target/myapp_mac/cv2/cv2.cpython-36m-darwin.so, 2)