1

I have followed this tutorial to install opencv: https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a

I have two different virtual environments setup.

On one cv2 works fine. On the other one I get:

ImportError: dlopen(/Users/me/.virtualenvs/py3cv/lib/python3.6/site-packages/cv2.so, 2): no suitable image found.  Did find:
/Users/me/.virtualenvs/py3cv/lib/python3.6/site-packages/cv2.so: mach-o, but wrong architecture
/usr/local/Cellar/opencv/3.4.1_5/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so: mach-o, but wrong architecture

the working interpreter shows:

Python 3.6.4 (default, Dec 25 2017, 14:57:56) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin

when I go into python. when I run import ctypes;print(ctypes.sizeof(ctypes.c_void_p)) I get: 8

The breaking one shows:

Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

when I run import ctypes;print(ctypes.sizeof(ctypes.c_void_p)): I get: 4

any ideas what is going wrong?

output of name -a

Darwin MyMBP 17.5.0 Darwin Kernel Version 17.5.0: Fri Apr 13 19:32:32 PDT 2018; root:xnu-4570.51.2~1/RELEASE_X86_64 x86_64
KillerSnail
  • 3,321
  • 11
  • 46
  • 64
  • The next line from the *Python* prompt would be helpful. The architecture of the *.so* file doesn't seem to match the one of the *Python* executable that tries to load it. Try running `file path_to_python_executable` (for both of your *VEnv*s) and also `file /Users/me/.virtualenvs/py3cv/lib/python3.6/site-packages/cv2.so`. – CristiFati May 08 '18 at 09:14
  • added i the next line – KillerSnail May 08 '18 at 09:20
  • What's the architecture of your *OSX* ? Check that by typing in the shell: `uname -m` (or better: `uname -a`). Also in *Python* prompt (for both instances) add: `import ctypes;print(ctypes.sizeof(ctypes.c_void_p))`. – CristiFati May 08 '18 at 09:26
  • added in the architecture and the ctypes – KillerSnail May 08 '18 at 09:37

1 Answers1

1

For an .so (shared object) to be successfully loaded by an executable, their architectures must match (on OSX, this can be extended to: the .so must "contain" the executable's architecture - as an .so can bundle multiple architectures inside).

The OpenCV version that you installed is built for Intel 064bit (pc064). That's why it works on the 064bit Python, but doesn't on 032bit one.

For more details on Python architecture, check [SO]: How do I determine if my python shell is executing in 32bit or 64bit mode on OS X? (@CristiFati's answer).
Also, [SO]: Install win32com on MacOs and Linux (@CristiFati's answer) might contain some useful info.

CristiFati
  • 38,250
  • 9
  • 50
  • 87