Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
changkun opened this issue Oct 4, 2015 · 25 comments
Labels

Comments

@changkun
Copy link

changkun commented Oct 4, 2015

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?

@alalek
Copy link
Member

alalek commented Oct 4, 2015

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

@changkun
Copy link
Author

changkun commented Oct 5, 2015

@alalek Thanks a lot! It works for me, but why it works?

@alalek
Copy link
Member

alalek commented Oct 12, 2015

Static build doesn't use RPATH at all

@AllanChen
Copy link

@alalek I have same question , i have try to add cmake -DBUILD_SHARED_LIBS=OFF ... to build again . it still not work!!!

@alalek
Copy link
Member

alalek commented Oct 14, 2015

@AllanChen Try to cleanup build folder and build from scratch.

@AllanChen
Copy link

@alalek you mean build again ??

@alalek
Copy link
Member

alalek commented Oct 14, 2015

Yes, build from scratch and install it again.

@AllanChen
Copy link

@alalek I need to uninstall opencv on my mac ?? or just remove build folder and rebuild all this .

@alalek
Copy link
Member

alalek commented Oct 14, 2015

Actually, there are not enough details about what have been installed and how you did this. Also you didn't put error message (where path is showed, like /usr/local/lib/python2.7/site-packages/cv2.so).

Rebuild is not enough in case of path like /usr/local/lib/python2.7/site-packages/cv2.so. You need to run "install" build target or something else.

BTW, you can use PYTHONPATH environment variable to point into build directory (like PYTHONPATH=<opencv-build-dir>/lib)

@AllanChen
Copy link

@alalek Thank you for your comment, i have already rebuild opencv (opencv 3.0) and add path on ~/.bash_profile file. it still not work. this is my install command and setup .

About Install

  • build opencv source code on opencv3.0 folder.
  • Command :
    cmake -D BUILD_SHARED_LIBS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
  • make
  • sudo make install

About Setting

  • add export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
    on ~/.bash_profile and source .bash_profile.
  • ln cv.py , cv2.so to /usr/local/lib/python2.7/site-packages (`/usr/local/lib/python2.7/site-packages have cv.py,cv2.so two files);
  • create test.py file
import cv

About Error

After i import cv lib still got import cv2 ImportError: No module named cv2
this error , when i add system path ,i got unsafe use of relative rpath lib/libopencv_shape.3.0.dylib in /usr/local/lib/python2.7/site-packages/cv2.so with restricted binary

Please Help .

@alalek
Copy link
Member

alalek commented Oct 16, 2015

@AllanChen Thanks! Commands looks good to me!

Could you add results of otool -L <build_dir>/lib/cv2.so and otool -L /usr/local/lib/python2.7/site-packages/cv2.so commands?
Does this work from the build dir: PYTHONPATH=lib python -c "import cv2; print cv2.__version__"

@AllanChen
Copy link

@alalek After i run otool -L opencv-3.0.0/build/lib/cv2.so

opencv-3.0.0/build/lib/cv2.so:
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/cv2.so (compatibility version 0.0.0, current version 0.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_shape.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_superres.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_stitching.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_videostab.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_objdetect.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_photo.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_video.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_calib3d.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_features2d.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_flann.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_ml.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_highgui.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_videoio.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_imgcodecs.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_imgproc.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /Users/Allan/Desktop/opencv-3.0.0/build/lib/libopencv_core.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)

Run otool -L /usr/local/lib/python2.7/site-packages/cv2.so

usr/local/lib/python2.7/site-packages/cv2.so:
    cv2.so (compatibility version 0.0.0, current version 0.0.0)
    lib/libopencv_shape.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_superres.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_stitching.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_videostab.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_objdetect.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_photo.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_video.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_calib3d.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_features2d.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_flann.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_ml.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_highgui.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_videoio.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_imgcodecs.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_imgproc.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    lib/libopencv_core.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)

Run PYTHONPATH=lib python -c "import cv2; print cv2.__version__" on build folder
3.0.0

It's very strange, i can run py command but i can not run this script on build folder!!!!! i have already ln the lib to site-packages!!!! something i miss ?????

Thanks Again ~~~~~!!!!!

@alalek
Copy link
Member

alalek commented Oct 16, 2015

It is very strange, because I have this output with your commands:

otool -L lib/cv2.so 
lib/cv2.so:
    /Users/build/projects/build/lib/cv2.so (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)
    /System/Library/Frameworks/QTKit.framework/Versions/A/QTKit (compatibility version 1.0.0, current version 1.0.0)
    /System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.8.0)
    /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1265.19.0)
    /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 20.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 855.14.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.8.0)
    /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1056.13.0)

There are no opencv_*.dylib files, as expected after using of -DBUILD_SHARED_LIBS=OFF.

What version of CMake on your machine?

cmake --version

Did you try to cleanup build folder before CMake run?

rm -rf /Users/Allan/Desktop/opencv-3.0.0/build

BTW, You can see exact path of the loaded Python module via __file__ field: print cv2.__file__.

@AllanChen
Copy link

@alalek Hei man , I think i understand why i can not link this lib ~~~!!!!, i have already run this script on build folder ,it work !!! you have to cp cv2.so to /usr/local/lib folder not rebuild opencv !!!!!

opencv3.0.0 can not cp cv2.so to /usr/local/lib , you need to cp this file ~~!!!!!

Now this is my code (cp cv2.so lib to /usr/local/lib)

import sys
sys.path.append('/usr/local/lib')
import cv2

If Not

import sys
sys.path.append(<link to your opencv build lib>)
import cv2

Thanks for your command ~~~!!!

@haptork
Copy link

haptork commented Oct 21, 2015

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()

@pranny
Copy link

pranny commented Nov 4, 2015

The problem arises due to the fact that cv2.so from python site-packages tries to link to a file which can not be found. The reason for it not to be found is because brew installs the file in different locations. Here are the set of steps to resolve this.

Find if your cv2.so links anywhere

ls -l /usr/local/lib/python2.7/site-packages/

Among other things, it should show a

3056 -rwxr-xr-x    1 admin  1563612 Oct 23 00:33 cv2.so

If this is the case, your cv2.so needs to be linked.
Check your python packages for opencv from brew directory.

ls -l /usr/local/Cellar/opencv/2.4.12/lib/python2.7/site-packages/cv2.so

If this file exists, it is the file that needs to be linked. If this file does not exist then (a) Your versions may be different, so change that accordingly. (b) Your opencv installation may have issues. Try uninstalling and reinstalling opencv again
Link the file from brew directory

ln -s -f /usr/local/Cellar/opencv/2.4.12/lib/python2.7/site-packages/cv2.so  /usr/local/lib/python2.7/site-packages/cv2.so

This should correctly link your cv2.so file thus resulting in removing this error.

@AllanChen
Copy link

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

@mahesh557
Copy link

mahesh557 commented Jul 22, 2016

In my case, i had to fix the relative path to absolute path fixed the issue

sh-3.2# otool -L /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/Pg/Pg.bundle
/Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/Pg/Pg.bundle:
    libpq.5.dylib (compatibility version 5.0.0, current version 5.8.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
sh-3.2# 
sh-3.2# install_name_tool -change libpq.5.dylib /usr/lib/libpq.5.dylib /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/Pg/Pg.bundle
sh-3.2# otool -L /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/Pg/Pg.bundle
/Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/Pg/Pg.bundle:
    /usr/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.8.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

@jeannieiscool
Copy link

I have similar issue here ...

ImportError: dlopen(/Applications/Anaconda/anaconda/envs/tensorflow/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so, 2): Library not loaded: lib/libopencv_shape.3.1.dylib
  Referenced from: /Applications/Anaconda/anaconda/envs/tensorflow/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so
  Reason: image not found

I'm trying to use opencv in tensorflow env following steps from here. but got this error above.
Any recommendation ?

https://www.scivision.co/anaconda-python-opencv3/

@arpita8
Copy link

arpita8 commented Jan 5, 2017

screen shot 2017-01-05 at 4 09 30 pm

i was able to import cv last time but it is not working this time.

Thanks

@drhirsch
Copy link

drhirsch commented Jan 11, 2017

@arpita8 does

from cv2 import cv

Work?

@arpita8
Copy link

arpita8 commented Jan 11, 2017

its still not working.

@AllanChen
Copy link

@arpita8 You can check openCV system path after El Capitan, some lib path need to reset and you need to run some command to relink it . You can check @pranny comment.

@RodenLuo
Copy link

RodenLuo commented Apr 7, 2017

Similar error as @jeannieiscool

After following macOS: Install OpenCV 3 and Python 3.5, I got:

ImportError                               Traceback (most recent call last)
<ipython-input-1-72fbbcfe2587> in <module>()
----> 1 import cv2

ImportError: dlopen(/Applications/anaconda/envs/pimage3/lib/python3.5/site-packages/cv2.so, 2): 
Library not loaded: libhdf5.10.dylib
  Referenced from: /usr/local/lib/libopencv_hdf.3.2.dylib
  Reason: image not found

Any help would be appreciated!

@chitrang6
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests