You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac El Capitan系统Python2.7安装OpenCV后cv2导入报错解决方法

Fixing ImportError: _clock_gettime with OpenCV and Python 2.7 on Mac El Capitan

Hey there, let's tackle that annoying import error you're facing. The root of the problem is straightforward: Mac OS X 10.11 El Capitan doesn't include the _clock_gettime system symbol, but the pre-built opencv-python wheels from PyPI are compiled for newer macOS versions that do have this function. Here are two reliable ways to fix it:

Method 1: Use Homebrew to Install a Compatible OpenCV Version

This is the quickest fix if it works for your setup:

  1. Clean up all existing OpenCV installations first to avoid conflicts:

    • Uninstall the PyPI version:
      pip uninstall -y opencv-python
      
    • Force-uninstall any Homebrew OpenCV versions:
      brew uninstall --force opencv
      
    • Delete leftover cv2 files from your Python site-packages:
      rm -rf /Library/Python/2.7/site-packages/cv2
      
  2. Install a legacy OpenCV version compatible with El Capitan
    OpenCV 3.x is the last major release that supports Python 2.7, and Homebrew has a dedicated formula for it:

    brew install opencv@3
    
  3. Link OpenCV's Python bindings to your system Python 2.7
    First, get the installation path of opencv@3:

    brew --prefix opencv@3
    

    (This should output something like /usr/local/opt/opencv@3)

    Then create a symbolic link to connect the OpenCV bindings to your Python's site-packages:

    ln -s /usr/local/opt/opencv@3/lib/python2.7/site-packages/cv2 /Library/Python/2.7/site-packages/cv2
    

    Finally, add OpenCV's library path to your system so Python can find it:

    echo /usr/local/opt/opencv@3/lib >> /etc/paths.d/opencv3
    
  4. Test the installation
    Open a Python 2.7 shell and run:

    import cv2
    print(cv2.__version__)
    

    If you see the version number (like 3.4.x), you're good to go!

Method 2: Compile OpenCV from Source (For Full Control)

If the Homebrew method doesn't work, compiling from source guarantees compatibility with El Capitan:

  1. Install build dependencies

    brew install cmake pkg-config numpy
    
  2. Clean up existing installations (same as step 1 in Method 1)

    pip uninstall -y opencv-python
    brew uninstall --force opencv
    rm -rf /Library/Python/2.7/site-packages/cv2
    
  3. Clone the correct OpenCV source versions
    We'll use OpenCV 3.4.16, the final release that supports Python 2.7:

    # Clone OpenCV main repo
    git clone https://github.com/opencv/opencv.git
    cd opencv
    git checkout 3.4.16
    
    # Clone the extra modules repo (matches the main version)
    git clone https://github.com/opencv/opencv_contrib.git ../opencv_contrib
    cd ../opencv_contrib
    git checkout 3.4.16
    
  4. Configure the build with CMake
    Create a build directory and run CMake with Python 2.7-specific settings:

    cd ../opencv
    mkdir build
    cd build
    
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=/usr/local \
          -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
          -D PYTHON2_EXECUTABLE=/usr/bin/python \
          -D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
          -D PYTHON2_LIBRARY=/usr/lib/libpython2.7.dylib \
          -D PYTHON2_NUMPY_INCLUDE_DIRS=/Library/Python/2.7/site-packages/numpy/core/include \
          -D BUILD_opencv_python2=ON \
          -D BUILD_opencv_python3=OFF \
          -D WITH_FFMPEG=OFF \
          ..
    
  5. Compile and install
    Use the -j flag with the number of CPU cores you have (e.g., -j4 for 4 cores) to speed up compilation:

    make -j4
    sudo make install
    
  6. Test the installation
    Run this command to verify:

    python -c "import cv2; print(cv2.__version__)"
    

A quick note: Python 2.7 is no longer supported by the Python Software Foundation, so if you can, consider migrating to Python 3 for long-term stability. But if you need to stick with 2.7, either of these methods should resolve the _clock_gettime error.

内容的提问来源于stack exchange,提问作者marc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:30:01