Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I installed opencv using these commands on my mac:

$ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2
$ tar xjvf OpenCV-2.4.3.tar.bz2
$ cd OpenCV-2.4.3
$ mkdir build; cd build
$ cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 -D CMAKE_C_FLAGS=-m32 -D CMAKE_CXX_FLAGS=-m32 ..
$ make -j4
$ sudo make install

then I tried to use it by importing it but got the error below:

~/opencv-2.4.4/build $ python
Python 2.7.3 (default, Mar 28 2013, 14:31:14) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/cv.py", line 3, in <module>
    from cv2.cv import *
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): no suitable image found.  Did find:
    /usr/local/lib/python2.7/site-packages/cv2.so: mach-o, but wrong architecture

Updates

While compiling OpenCV I noticed that its picking up python libraries from a previous version (one installed by apple):

-- Could NOT find PythonLibs: Found unsuitable version "2.7.2", but required is exact version "2.7.3" (found /usr/lib/libpython2.7.dylib)

--   Python:
--     Interpreter:                 /usr/local/bin/python2 (ver 2.7.3)
--     Libraries:                   /usr/lib/libpython2.7.dylib (ver 2.7.2)
--     numpy:                       /usr/local/lib/python2.7/site-packages/numpy/core/include (ver 1.7.0)
--     packages path:               lib/python2.7/site-packages

How do I fix this?

share|improve this question
    
OK, you clearly have some third-party Python 2.7 in addition to Apple's Python 2.7. Most likely, what happened is that OpenCV built for Apple's, and you're running it with another. Without knowing what you installed, and exactly how and why and what your PATH looks like in different scenarios and so on, it's very hard to say exactly what, but I'm guessing python means the third-party for you, but inside sudo it means the Apple one. –  abarnert Mar 28 '13 at 20:22
    
Wait… why did you explicitly ask OpenCV to build for i386? If you want to build a 32-bit OpenCV, you have to use it with a 32-bit Python. Apple's Python, and most others, are universal (32-/64-bit) so it's just a matter of starting them differently, while if you have a 64-bit-only Python you may have to rebuild… but either way, I have to wonder why you want 32-bit. –  abarnert Mar 28 '13 at 20:24
    
I installed python by brew install python and its version 2.7.3. from the error it looks like its complaining about architecture. I am on 64 however, I gave tags for 32 while compiling opencv. perhaps that is an issue. I am compiling and installing opencv again after removing those tags. Will update –  birdy Mar 28 '13 at 20:25
    
we both spotted same thing :). yeah, i don't want 32 bit. I took that command from the web somewhere and stupidly copy pasted it... –  birdy Mar 28 '13 at 20:26
    
Well, you probably have both problems. Watch OpenCV as it compiles and see which Python it's linking against, and I'll bet you it's Apple's. (If you see anything about version 2.7.2, or a path in /System, or in /usr/lib instead of /usr/local/lib… that's Apple's Python.) So, you will get past this problem only to crash a little bit later. –  abarnert Mar 28 '13 at 20:26

1 Answer 1

up vote 1 down vote accepted

The error tells you that the problem is "mach-o, but wrong architecture". That generally means that you're trying to load a 32-bit library in a 64-bit app, or vice-versa (or similarly, for Intel vs. PowerPC).

Whichever Python you've installed, it's likely that you got a universal build that can run as either a 32-bit app or a 64-bit app, but by default it runs as 64-bit. But you're explicitly building OpenCV for 32-bit:

$ cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 -D CMAKE_C_FLAGS=-m32 -D CMAKE_CXX_FLAGS=-m32 ..

If that was a mistake, just rebuild and don't do that.

If you want it to be 32-bit, you can start a Universal python in 32-bit mode, or install a 32-bit Python.


Meanwhile, you're trying to run this with a Homebrew-installed Python 2.7.3, but there's a good chance that you're building it against Apple's pre-installed 2.7.2 (or, worse, partially building it one way, partially another).

First, the usual way people add Homebrew to their PATH doesn't affect any commands run with sudo, so if sudo make install does anything to find your Python, it will likely find a different one than cmake and make did.

Second, both Apple's Python and Homebrew's think they own /usr/local/lib/python2.7 (and also think they have exclusive rights to install tools/scripts like /usr/local/bin/pip-2.7), which makes this even more painful.

share|improve this answer
    
What a mess...Any way I can get rid of apple's python completely? –  birdy Mar 28 '13 at 20:33
    
No, you definitely cannot get rid of Apple's python. You may break the OS, and the next OS update will replace it anyway. Is there a reason you can't get rid of the Homebrew Python? –  abarnert Mar 28 '13 at 20:35
    
no reason. I just wanted to start with the latest version so I went with brew install python. Should I just remove it by brew uninstall python –  birdy Mar 28 '13 at 20:38
    
Otherwise, you have to figure out why OpenCV's configure process is picking up Apple's Python, and then you can figure out how to change your configuration or work around it. The problem may just be that OpenCV doesn't understand framework builds, and creating a symlink from your Cellar's Python.framework/Versions/2.7/Python to /usr/local/lib/libpython2.7.dylib will fix it. –  abarnert Mar 28 '13 at 20:39
    
You may want to just brew unlink python so you can restore it later if you change your mind (and, for that matter, you can continue to use it by using an explicit path into the Cellar). But yeah, I'd remove it. Many people disagree with me, but see here for my reasons. Or just look at the mess you're having and what it'll take to debug it for reasons. –  abarnert Mar 28 '13 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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