4

I spent the past couple of days trying to get opencv to work with my Python 2.7 install. I kept getting an error saying that opencv module was not found whenever I try "import cv".

I then decided to try installing opencv using Macports, but that didn't work.

Next, I tried Homebrew, but that didn't work either.

Eventually, I discovered I should modify the PYTHONPATH as such: export PYTHONPATH="/usr/local/lib/python2.6/site-packages/:$PYTHONPATH"

My problem is that I didn't find /usr/local/lib/python2.*...etc The folder simply doesn't exist

So my question is this: How do I properly install Python on OS X Snow Leopard for it to work with opencv?

Thanks a lot,

4 Answers 4

7

I spent a couple days on this myself. For me, the problem was that that OpenCV installer was not finding the right python installation. It was defaulting to the MacOS-installed version despite the fact that I had upgraded python with homebrew and was using a virtualenv for python. I have collected most of my setup in a gist here: https://gist.github.com/4150916

Use homebrew to get all the dependencies, but then download the OpenCV tarball and compile yourself being sure to specify all the python related configuration options.

Assuming a virtualenv named 'opencv'...

cd OpenCV-2.4.3/
mkdir release
cd release
cmake -D PYTHON_EXECUTABLE=$WORKON_HOME/opencv/bin/python \
 -D PYTHON_PACKAGES_PATH=$WORKON_HOME/opencv/lib/python2.7/site-packages \
 -D INSTALL_PYTHON_EXAMPLES=ON\
 -D PYTHON_INCLUDE_DIR=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Headers\
 -D PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib\
..
make -j8
make install
2
  • I am trying this with OpenCV-2.4.6 and Python 2.7.4 and get this error with cmake: CMake Error: The source directory "/Users/hdara/src/opencv-2.4.6/release/PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib.." does not exist.
    – haridsv
    Aug 8, 2013 at 10:39
  • OK, I found the issue, it was just a missing space before ... There were missing spaces that I added, but failed to notice this. The recipe seems to have the missing spaces.
    – haridsv
    Aug 8, 2013 at 11:05
2

You need to install the module using your python2.7 installation. Pointing your PYTHONPATH at stuff installed under 2.6 to run under 2.7 is a Bad Idea.

Depending on how you want to install it, do something like python2.7 setup.py or easy_install-2.7 opencv to install.

fwiw, on OS X the modules are usually installed under /System/Library/Frameworks/Python.framework/ but you should almost never need to know where anything installed in your site packages is physically located; if Python can't find them without help you've installed them wrong.

5
  • I did try changing 2.6 to 2.7, but it didn't work because, as I said, the entire directory doesn't exist. I installed python from the .dmg image provided on the official Python website.
    – HHH
    Aug 20, 2011 at 1:41
  • 1
    uh, yes. it wouldn't be in /usr/local/lib because it's in /System/Library/Frameworks/, but don't try to use it from where the system python installed it; install it again with the python you want.
    – Wooble
    Aug 20, 2011 at 1:45
  • After referring to a comment on this post: stackoverflow.com/questions/5846745/opencv-python-osx I tried this: sudo port install python27 then this: sudo port select --set python python27 It didn't return any errors, but now I'm stuck on py27-numpy. Macports returns this error: Unable to open port: tar: Error opening archive: Failed to open '/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/python/py27-numpy': No such file or directory Any ideas? (thnx BTW)
    – HHH
    Aug 20, 2011 at 1:49
  • Have you modified your shell PATH to include /opt/local/bin which is where MacPorts installs commands? See trac.macports.org/wiki/… For the numpy issue, make sure you clean the port and update to the latest versions: sudo port clean py27-numpy; sudo port selfupdate; sudo port install py27-numpy
    – Ned Deily
    Aug 20, 2011 at 4:45
  • After following the mentioned guide, opencv now works (even without numpy). I will give it one last try as you mentioned. Thanks!
    – HHH
    Aug 20, 2011 at 12:42
1

Installing OpenCV with Homebrew

brew tap homebrew/homebrew-science
brew install opencv

Setting up Python

Depending on your install location - OS X Default

cd /Library/Python/2.7/site-packages/

or - Homebrew Python

cd /usr/local/lib/python2.7

Then create the symbolic link

ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

The above method sourced from a blog post.

2
  • in a perfect world only. opencv installation on mac is feisty by default. Jan 7, 2015 at 17:04
  • I've had great results with brew and opencv installation as specified. You will need to link in the python bits. Jan 13, 2015 at 8:30
1

I searched and tried installing opencv3 with python3 for 3 days. Some links suggest for Brew and some virtual env, some say install xcode but all failed in my case. Dont use linux steps to instal opencv-python on Mac. Problem with Mac is Python 2.7 is already installed by Mac. On top of that installing and linking all site-packages is little problematic and we end up with errors.

I'll share what I did: easy steps to install complete package opencv3, numpy, matplotlib, notebook, spyder etc.. on Mac.

  1. Install anaconda, it creates a directory and install everything inside that use this link -> https://www.continuum.io/downloads download command-line-install

  2. After download, goto terminal and download location of anaconda. $ bash Anaconda3-4.3.0-MacOSX-x86_64.sh

  3. Installation will ask you to append path to .bash_profile >> say yes

  4. Goto home directory, run .bash_profile $ source .bash_profile

  5. check python, should be pointing to $ which python $ /.../anaconda/bin/python

  6. Last step $ pip install opencv-pyhton

$ python

$ import cv2

if no errors, we are good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.