1

I'm trying to import opencv2 using python3.7.3 on Mac OS 10.10 but an ImportError occurs. How should I solve it?

I tried to install opencv3 using pip, pip3, and homebrew. Probably not install right.

import cv2
import numpy as np
import sys
import pytesseract as py
import matplotlib.pyplot as plt

Error

Traceback (most recent call last):
  File "/Users/wujian/Desktop/Project/Proj.py", line 1, in <module>
    import cv2
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: _clock_gettime
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib (which was built for Mac OS X 10.12)
  Expected in: /usr/lib/libSystem.B.dylib
 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Hao
  • 13
  • 5

1 Answers1

1

The issue seems to be (oddly) related to your copy of libavutil which is part of ffmpeg.

It's hinted at by this part of the error message:

ImportError: dlopen(/.../cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: _clock_gettime
  Referenced from: /...python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib (which was built for Mac OS X 10.12)

Installing OpenCV requires ffmpeg. If you installed it via homebrew, it's listed as a dependency.

$ brew info opencv
...
==> Dependencies
Build: cmake ✓, pkg-config ✓
Required: eigen ✓, ffmpeg ✓, glog ✓, ...

Installing ffmpeg will include the libavutil library, which can be checked by downloading pre-built shared libraries or by Homebrew:

$ brew list ffmpeg | grep libavutil.*dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.56.22.100.dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.56.dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.dylib

Now, your problem is that the libavutil (and ffmpeg) on your system is not backwards-compatible with your Mac 10.10. It was built targeting a newer Mac 10.12, which seems to have introduced changes to the clock_gettime API, causing the "Symbol not found" error. This has also been reported to the opencv-python issues:

This is not related to OpenCV. The error comes from libavutil (=FFmpeg) which is installed via homebrew. The error is related to binary backward compatibility (apparently Apple has changed the libsystem interface). I'm not too familiar with Apples ecosystem but building FFmpeg against earlier OS version might fix the issue if the older symbols are present also in the latest OS versions. Full backward compatibility on macOS might be impossible to achieve.


I was just bitten by this issue the hard way deep in nested dependencies; FFmpeg targets 10.12 but I'm on 10.11. Hoping for a fixed release soon.

Note that while downgrading Xcode may work too, the proper way to target an earlier version of macOS is to add e.g. -mmacosx-version-min=10.11 or -mmacosx-version-min=10.6 to CFLAGS/CXXFLAGS/LDFLAGS; this will tune headers, compiler and linker to produce a binary that is compatible with at least that version of macOS.

The only solution it seems, is to install an older version of ffmpeg (with libavutil) that is compatible with your Mac 10.10, and then tell OpenCV to link to that older version. You'll probably need to also build OpenCV from source (see Configure and Build OpenCV to Custom FFMPEG Install).

You can also try checking this post over at SuperUser:
Which ffmpeg package I should download for macOS?

  • Try installing the older ffmpeg@2.8 from Homebrew (though I'm not sure if that's going to work, I don't have a Mac 10.10 to test it on).
  • Try downloading pre-built static/shared builds from ffmpeg.org.
  • Try building it from source (see CompilationGuide/macOS)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135