Mac El Capitan系统Python2.7安装OpenCV后cv2导入报错解决方法
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:
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
- Uninstall the PyPI version:
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@3Link OpenCV's Python bindings to your system Python 2.7
First, get the installation path ofopencv@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/cv2Finally, add OpenCV's library path to your system so Python can find it:
echo /usr/local/opt/opencv@3/lib >> /etc/paths.d/opencv3Test 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:
Install build dependencies
brew install cmake pkg-config numpyClean 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/cv2Clone 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.16Configure 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 \ ..Compile and install
Use the-jflag with the number of CPU cores you have (e.g.,-j4for 4 cores) to speed up compilation:make -j4 sudo make installTest 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

