简体   繁体   English

Python-opencv错误导入cv2 ImportError:更新OS X El Capitan后的dlopen

[英]Python-opencv Error import cv2 ImportError: dlopen after update of OS X El Capitan

I have this error when import cv2 after update the system of OS X El Capitan 更新OS X El Capitan系统后导入cv2时出现此错误

import cv2

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

I have tried the method in Cannot import cv2 because unsafe use of relative rpath lib in cv2.so with restricted binary 我已经尝试过无法导入cv2中的方法, 因为在cv2.so中使用受限二进制文件不安全地使用了相对rpath lib

rebuild build use  cmake -D BUILD_SHARED_LIBS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

But it doesn't work. 但这是行不通的。

This is because of SIP (System Integrity Protection) introduced in El Capitan link . 这是因为El Capitan link中引入了SIP(系统完整性保护)。

I too faced the same issue and came across this SO link . 我也面临同样的问题,并遇到了这个SO 链接 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. 在opencv库中有大量这些需要纠正。 You can optionally disable SIP. 您可以选择禁用SIP。 I preferred to change the links instead and wrote the following python snippet. 我更喜欢更改链接,并编写了以下python代码段。

Change the ABSPATH and LIBPATHS if required. 如果需要,请更改ABSPATH和LIBPATHS。 It can be used for any other libraries as well. 它也可以用于任何其他库。 It will create rPathChangeCmd.txt which you can paste in the terminal. 它将创建rPathChangeCmd.txt,您可以将其粘贴到终端中。 It will also create rPathChangeErr.txt in case of any errors. 如果出现任何错误,它还将创建rPathChangeErr.txt。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM