简体   繁体   中英

Installing OpenCV 3 for Python 3 on a mac using Homebrew and pyenv

I am running Mac OS X 10.11 (El Capitan). I want to:

  • Maintain my system version of Python as the default
  • Install Python 3.5 alongside it
  • Install OpenCV 3 and the Python bindings

I installed pyenv and Python 3.5 by following this SO answer: https://stackoverflow.com/a/18671336/1410871

I activated my Python 3 installation and installed OpenCV 3 with Python 3 support like this:

pyenv shell 3.5.0
brew install opencv3 --with-python3

But when I launch an IPython shell and import cv2 , I get an error:

ImportError                               Traceback (most recent call last)
<ipython-input-1-72fbbcfe2587> in <module>()
----> 1 import cv2

ImportError: No module named 'cv2'

why?

Answering my own question: I have to manually create a symlink to the shared object file and place it in the pyenv Python 3 site-packages directory:

ln -s /usr/local/opt/opencv3/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so ~/.pyenv/versions/3.5.0/lib/python3.5/site-packages/cv2.so

Now the line import cv2 works as expected in Python.

opencv3 is keg only and does not get linked when installing via Brew

Do the following:

$ brew link opencv3 --force

This is a more controlled approach than creating symbolic links by hand.

Then you'll be able to:

$ python3
import cv2

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM