Cannot install openCV 3.1.0 with python3. CMAKE not including or linking python correctly

14,949

Solution 1

I fixed my problem by deleting the entire /build directory, and running cmake again (with -D BUILD_opencv_python3=yes). I think it gave an error because of some sort of caching from cmake, so when I deleted everything and started over again, the error was gone.

Solution 2

The above solutions didn't work for me, I had to specify a whole bunch of options to make cmake recognize python3. This did the trick:

cmake {...} -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_C_EXAMPLES=OFF \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D BUILD_EXAMPLES=ON \
  -D PYTHON3_EXECUTABLE=$(which python3) \
  -D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
  -D PYTHON_INCLUDE_DIR2=$(python3 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") \
  -D PYTHON_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") \
  -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())") \
  -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") 

Solution 3

I believe, the issue is raised by internal bug from cmake-gui; OPENCV_PYTHON_VERSION variable is considered as Boolean, while cmake scripts consider the variable as placeholder for your preferred python version. Thus, delete the variable and re-added manually as string datatype with an initial value of your preferred python version. e.g., 3.7. Once you press Generate, cmake will accept python module with your filled libraries and include variables. However, if you're required to press Generate again, do that freely without modifying the OPENCV_PYTHON_VERSION variable as the cmake gui will return it as Boolean again, while your inserted preferred value is maintained and cached internally.

Solution 4

you can enable cmake output ou CmakeVars to check whether python2 or python3 are unavailable (look for it)

if this is the case, running cmake again wont make the trick, unless you clear CMakeCache.txt and .cmake files from build folder you may leave the rest to not compile everything again

As I am using inside docker i disabled some other things, but you may use this if you wish but adjust the variables as you need

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_OPENCL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.2/modules -D WITH_LIBV4L=OFF -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=OFF -D WITH_DC1394=OFF -D ENABLE_NEON=OFF -D OPENCV_ENABLE_NONFREE=ON  -D WITH_PROTOBUF=OFF -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_OPENCV_PYTHON3=yes  -D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON_INCLUDE_DIR2=$(python3 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") -D PYTHON_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")  ..

Solution 5

if you are on ARM64 this may be the problem: https://github.com/numpy/numpy/issues/18131

setting OPENBLAS_CORETYPE=ARMV8 solved cmake not selecting python3

Share:
14,949
user3667125
Author by

user3667125

Updated on June 14, 2022

Comments

  • user3667125
    user3667125 almost 2 years

    I'm trying to get OpenCV 3.1.0 installed with Python3 on my machine. Because I have Ubuntu 16.04, I am following this guide exactly:

    http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

    However, I have trouble in step 4, after running cmake. The output of my cmake program has this snippet:

    --   OpenCL:
    --     Version:                     dynamic
    --     Include path:                /home/kevin/opencv-3.1.0/3rdparty/include/opencl/1.2
    --     Use AMDFFT:                  NO
    --     Use AMDBLAS:                 NO
    -- 
    --   Python 2:
    --     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python (ver 3.5.2)
    -- 
    --   Python 3:
    --     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python3 (ver 3.5.2)
    -- 
    --   Python (for build):            /home/kevin/.virtualenvs/cv/bin/python
    -- 
    --   Java:
    --     ant:                         NO
    --     JNI:                         /usr/lib/jvm/java-8-oracle/include /usr/lib/jvm/java-8-oracle/include/linux /usr/lib/jvm/java-8-oracle/include
    --     Java wrappers:               NO
    --     Java tests:                  NO
    

    The cmake file doesnt seem to properly link the missing properties under the python section (libraries, numpy, packages_path). I tried compiling opencv and installing it, and when I load python3 and import cv, it says it could not find the cv package.

    I tried modifying the CMAKE command, so instead of using what the guide suggested, I added three parameters to force cmake to include the missing python properties (the last three properties were my modification):

    cmake -D CMAKE_BUILD_TYPE=RELEASE     \
    -D CMAKE_INSTALL_PREFIX=/usr/local     \
    -D INSTALL_PYTHON_EXAMPLES=ON     \
    -D INSTALL_C_EXAMPLES=OFF     \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules     \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python     \
    -D BUILD_EXAMPLES=ON    \
    -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so    \
    -D PYTHON_NUMPY_INCLUDE_DIR=~/.virtualenvs/cv/lib/python3.5/site-packages/numpy/core/include    \
    -D PYTHON_PACKAGES_PATH=~/.virtualenvs/cv/lib/python3.5/site-packes ..
    

    But still, the output of cmake is the same (it only lists the interpretter, and no other python properties), and when I try building opencv and installing it, my python interpretter could not find the opencv package.

    My /usr/local/lib/python3.5/ file did NOT have a site-packages directory after I finished installing opencv either.

    Can anyone point me in the right direction? I've been trying to search the past few hours on how to fix this, but could not find a way so far, so I'd greatly appreciate any guidance. Thank you!

    Update

    I realize that I have to add -D BUILD_opencv_python3=yes to the cmake command so that opencv looks for the python libraries. However, this is the new output that I get from cmake:

    --   Python 3:
    --     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python (ver 3.5.2)
    --     Libraries:                   NO
    --     numpy:                       NO (Python3 wrappers can not be generated)
    --     packages path:               lib/python3.5/site-packages
    

    Any idea why cmake is not finding my python 3 libraries correctly (even when I add "-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so " to the cmake options)?

    Update

    I fixed the problem by deleting the entire /build directory, and running cmake on a fresh directory. I think the error continued to show up because of some caching done by cmake, but adding the flag -D BUILD_opencv_python3=yes seem to do the trick for me.

  • Nick Crews
    Nick Crews almost 7 years
    I had the same problem, this worked for me (Ubuntu 16.10, python3, Opencv3 with contrib)
  • Niaz Mohammed
    Niaz Mohammed over 5 years
    Thanks a bunch! I was using pipenv and had no idea where these locations were or how to find them.
  • Eyshika
    Eyshika about 4 years
    still not working . Libraries: NO -- numpy: NO (Python3 wrappers can not be generated) -- install path: -
  • Burak
    Burak almost 3 years
    @Eyshika On Windows, I was getting an error for PYTHON_LIBRARY. (get_config_var returns None) Manually providing the path (~/AppData/Local/Programs/Python/Python39/libs/python39.lib) solved the problem.