I am trying to use an alternative version of opencv (3.0) in my Python scripts. I compiled and installed this version to my home directory, and now I am trying to tell Python to use it. I have the standard opencv version (2.4.8) installed with apt-get.

My problem is that python always imports the 2.4.8 version, even when I add the dist-packages of version 3.0 at the beginning or at the end of PYTHONPATH. Deleting opencv 2.4.8 is no option.

How could I do that correctly?

1 Answer 1


Since OpenCV is a C library with python bindings rather than a python one, you need to tell the C environment to look in the correct library path, as well as the python one. To do that, use LD_LIBRARY_PATH. Assuming the OpenCV .so file is (say) $HOME/opencv-3.0/lib/libopencv.so.3, you do the following:

LD_LIBRARY_PATH=$HOME/opencv-3.0/lib PYTHONPATH=(...whatever...) python <script>
  • Do you know in which order the libraries are checked? /usr/lib is not part of LD_LIBRARY_PATH and so I think it is always checked before going to LD_LIBRARY_PATH, which would lead to using the older opencv version.
    – Mehdi
    Sep 15, 2015 at 10:33
  • nevermind, LD_LIBRARY_PATH is checked first.
    – Mehdi
    Sep 15, 2015 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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