Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 708
Daniel Golden Avatar asked Oct 19 '15 20:10

Daniel Golden


1 Answers

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.

like image 118
Daniel Golden Avatar answered Sep 21 '22 22:09

Daniel Golden