opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
78.9k stars 55.8k forks source link

OpenCVDetectPython.cmake cannot find correct python libs in venv #20783

Open braindevices opened 3 years ago

braindevices commented 3 years ago
System information (version)
Detailed description

After activate the venv and run cmake config:

Found PythonInterp: /opt/venv-74a3d1fd0900899b80dfa870337f6bb2/bin/python (found suitable version "3.9.5", minimum required is "2.7")
...
Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)

This problem is caused by the deprecated FindPythonLibs, it is too old and has many unfixed bugs. Please use FindPython which actually use python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())" to find the include dir and python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))" to find the libs.

from cmake document:

Deprecated since version 3.12: Use FindPython3, FindPython2 or FindPython instead.

since in the OpenCVDetectPython.cmake it already conditioned with cmake version 3.12+, I think we should use FindPython there when cmake version >= 3.12 for much better python support

Steps to reproduce
Issue submission checklist
jbnoel-gpsw commented 2 years ago

I have a similar issue when building OpenCV 4.6.0 on macOS 12.5.1 with Apple Silicon (arm64). OpenCVDetectPython.cmake fails to detect correctly Python 3 from Brew:

...
-- Detected processor: arm64
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.18", minimum required is "2.7") 
-- Found PythonLibs: /usr/lib/libpython2.7.dylib (found suitable exact version "2.7.18") 
-- Found PythonInterp: /opt/homebrew/bin/python3 (found suitable version "3.9.13", minimum required is "3.2") 
-- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is exact version "3.9.13")
...

The CMake "call stack" is

I provided a patch to the CMake project to fix CMakeFindFrameworks.cmake: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7618

The OpenCV project should move away from using FindPythonLibs.cmake and FindPythonInterp.cmake (both are deprecated since CMake 3.12)

FelixJongleur42 commented 10 months ago

The OpenCV project should move away from using FindPythonLibs.cmake and FindPythonInterp.cmake (both are deprecated since CMake 3.12)

You are referring to https://cmake.org/cmake/help/latest/policy/CMP0148.html - should we open another issue for this, or should the issue be dedicated to moving to the new modules?

current output of cmake

CMake Warning (dev) at cmake/OpenCVUtils.cmake:144 (find_package):
  Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
  are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.

Call Stack (most recent call first):
  cmake/OpenCVDetectPython.cmake:64 (find_host_package)
  cmake/OpenCVDetectPython.cmake:286 (find_python)
  CMakeLists.txt:660 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

Found PythonInterp: C:/Program Files/Python311/python.exe (found suitable version "3.11.4", minimum required is "3.2") 
CMake Warning (dev) at cmake/OpenCVDetectPython.cmake:140 (find_package):
  Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
  are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.

Call Stack (most recent call first):
  cmake/OpenCVDetectPython.cmake:286 (find_python)
  CMakeLists.txt:660 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

Found PythonLibs: C:/Program Files/Python311/libs/python311.lib (found suitable exact version "3.11.4") 
Ryanf55 commented 9 months ago

This presents a strong case to bump the minimum required CMake version to 3.12 in OpenCV5. Anyone compiling on a new Windows or MacOS machine may hit this now that those OS's will come with CMake >=3.27.

Has anyone looked into if FindPython3 will work?