'OpenCV Python - No module named cv2 (again)

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.



Solution 1:[1]

I did run in the same trouble. But looking at (and following) this tutorial it seems like splitting up the python environment did the trick.

Solution 2:[2]

I just installed OpenCV 3.0 on Ubuntu 14.04. It's working.

According to the official tutorial, install OpenCV.

#!/bin/bashd
#https://help.ubuntu.com/community/OpenCV
version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)"
echo "Installing OpenCV" $version
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -qq remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5-default checkinstall
echo "Downloading OpenCV" $version
wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
echo "Installing OpenCV" $version
unzip OpenCV-$version.zip
cd opencv-$version
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=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 ..
make -j2
sudo checkinstall
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV" $version "ready to be used"

enter image description here Use the following code for testing:

import cv2
img = cv2.imread("/home/xiao/Desktop/test.png")
cv2.imshow("opencv",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Solution 3:[3]

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

Solution 4:[4]

Just change your CMakeChahce file to write python include files, and lib dir to it. you may use find / -name "Python.h" to find your python directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DeDenker
Solution 2 yushulx
Solution 3
Solution 4 FelixSFD