opencv: Cannot import cv2 because unsafe use of relative rpath lib in cv2.so with restricted binary

I just update my laptop from Yosemite to El Capitan. When I try to use OpenCV-Python, I got this information:

➜  ~  python
Python 2.7.10 (default, Aug 22 2015, 20:33:39) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: lib/libopencv_shape.3.0.dylib
  Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
  Reason: unsafe use of relative rpath lib/libopencv_shape.3.0.dylib in /usr/local/lib/python2.7/site-packages/cv2.so with restricted binary
>>> exit()
➜  ~  which python
/usr/bin/python
➜  ~  echo $PYTHONPATH
/usr/local/lib/python2.7/site-packages/
➜  ~  

I installed OpenCV3.0 by compiling the source code not using homebrew. I find a similar problem description from homebrew-opencv: homebrew-science-issues but quite different with this.

What should I do now?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 25 (7 by maintainers)

Most upvoted comments

@pranny Thanks !!! it’s work for me

This is because of SIP (System Integrity Protection) introduced in El Capitan link.

I too faced the same issue and came across this SO link. Basically, the relative path dependencies listed in the shared libs need to be changed to absolute paths. There are huge number of these to be corrected in opencv libraries. You can optionally disable SIP. I preferred to change the links instead and wrote the following python snippet.

Change the ABSPATH and LIBPATHS if required. It can be used for other libraries as well. It will create rPathChangeCmd.txt which you can paste in the terminal. It will also create rPathChangeErr.txt in case of any errors. I would suggest check error file (if created) before pasting the commands.

import glob
import subprocess
import os.path

ABSPATH = "/usr/local/lib/"  # absolute path to relative libraries
# libraries to correct
LIBPATHS = ['/usr/local/lib/python2.7/site-packages/cv2.so', '/usr/local/lib/libopencv*'] 

PREFIX = 'sudo install_name_tool -change '

assert(ABSPATH.startswith('/') and ABSPATH.endswith('/'), 
    'please provide absolute library path ending with /')

libs = []
for path in LIBPATHS:
  libs += glob.glob(path)

cmd =  []
err = []
for lib in libs:
  if not os.path.isfile(lib):
    err.append(lib+" library not found") # glob should take care
  datastr = subprocess.check_output(['otool','-l','-v', lib])
  data = datastr.split('\n') 
  for line in data:
    ll = line.split()
    if not ll: continue
    if (ll[0] == 'name' and ll[1].endswith('.dylib') and not ll[1].startswith('/')):
      libname = ll[1].split('/')[-1]
      if os.path.isfile(ABSPATH+libname):  
        cmd.append(PREFIX+ll[1]+" "+ABSPATH+libname+' '+lib)
      else:
        err.append(ABSPATH+libname+" does not exist, hence can't correct: "+ll[1]+" in: "+lib)

ohandle = open("rpathChangeCmd.txt", 'w')
for lib in cmd:
  ohandle.write(lib+'\n')
ohandle.close()

if err:
  ehandle = open("rpathChangeErr.txt", 'w')
  for e in err:
    ehandle.write(e+'\n')
  ehandle.close()

Static build should help (via CMake): cmake -DBUILD_SHARED_LIBS=OFF ...

Solved this issue:

In conda currently, Python 3 doesn’t support opencv 3 or 2. So I changed from python 3 to 2.

and installed opencv by the following command

conda install -c menpo opencv=2.4.11

and did import cv2

and it is working