Jean-Pat
Jean-Pat

Reputation: 1871

Fails to build OpenCV 3 with Python2 support

I tried to build opencv 3 with python support (with cuda, too) for Ubuntu 14.04 using CMake-gui.

The compilation seems to work fine until from a Python console:

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2

I tried both with python3 enabled or disabled without change. The cmakeCache.txt is here.

Any idea to fix the problem?

Upvotes: 3

Views: 3696

Answers (2)

bio_c
bio_c

Reputation: 321

I had the same problem since yesterday, and I solved it as follows:

  1. Download OpenCV 3(beta, rc1) and install all the necessary dependencies, including Python dev package.
  2. Open Cmake GUI.
  3. Activate the Grouped checkbox.
  4. Select the OpenCV source and build directories (or just delete the cache if you previously configured).
  5. Press Configure. Select your preferred generator.
  6. Check/uncheck the options you need to (OpenGL support, TBB, etc.)
    • If you want, you can press Configure after every option modification to see if it fails or not. Just ignore the Python2 options for now.
  7. Once you have configured all other options, select the PYTHON2 group and delete it with Remove entry.
  8. Press Configure. The PYTHON2 group will reappear, in red. Do not change anything more, just press Generate.

Now you can make/compile the usual way. The cv2.so file will be created in build/lib/ directory. With make install it will be installed for Python 2.

If it still does not work maybe the problem is somewhere else. After step 8, the cmake output must include the lines (actual paths might be different in your case):

Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)
    Libraries:                   /usr/lib/i386-linux-gnu/libpython2.7.so (ver 2.7.6)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
    packages path:               lib/python2.7/dist-packages

In no case there can be a line saying NO, like:

Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)
    Libraries:                   NO
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
    packages path:               lib/python2.7/dist-packages

Hope this helps.

Upvotes: 4

Connor H
Connor H

Reputation: 346

If it compiles fine, then the probem isn't with cmake or the way your build options are set. The problem looks like it isn't with python2 or 3 either, but the way you have OpenCV installed. After you compile OpenCV you have to move your opencv python modules to /usr/local/lib or python won't be able to see them.

Upvotes: 1

Related Questions