OpenCV Python - No module named cv2 (again)

thomas picture thomas · May 20, 2015 · Viewed 11.8k times · Source

I know that this question has already been asked here a few times, but since no answer helped me so far, I'm posting it here again.

What I did:

I cloned the repository https://github.com/Itseez/opencv, created and changed to the build directory and compiled OpenCV using the following commands.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON -D BUILD_opencv_python2=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D CUDA_ARCH_BIN="2.0 3.0 3.5" ..
make
sudo make install

Everything compiled fine. Afterwards I added /usr/local/lib to /etc/ld.so.conf.d/opencv.conf and performed sudo ldconfig. Finally, I added

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

to my local bashrc. Everything worked fine and I also can execute the cpp examples, but as soon as I try a python example or try using OpenCV in python myself, I get:

ImportError: No module named cv2

Things I tried based on other posts:

  1. Manually copy cv2.so to /usr/local/lib/python2.7/dist-packages: Did not work because I cannot find cv2.so anywhere.

  2. Missing numpy or multiple versions of numpy: No problem here, because I only have one version of numpy.

Additional:

I also tried the same steps with the most recent sources from the homepage: same result.

I found several ressources saying that the correct flag is BUILD_NEW_PYTHON_SUPPORT now and not BUILD_PYTHON_SUPPORT, like it was in earlier version. Since both didn't work for me, I used BUILD_opencv_python2. Is that correct?

So, what could be the problem here? I mean, except the fact that there is no cv2.so anywhere on my machine. Btw, I use Ubuntu 14.04.

Thanks in advance.

Answer

Edgar H picture Edgar H · May 22, 2015

I'm not sure if this is also the case if you compile from source but when you use the self-extractor you need to copy a file containting the Python bindings, as @Malik Brahimi pointed out. Did you do this step?

  1. Goto opencv\build\python\2.7 folder
  2. Copy cv2.pyd to C:\Python27\lib\site-packeges

Here is a tutorial this is taken from and that worked for me with both Anaconda on Ubuntu 14.04 and WinPython. On Linux just copy the file to the same folder of your python distribution, i.e. Python27/lib/site-packeges