#20783·opencv

OpenCVDetectPython.cmake cannot find correct python libs in venv

Author: braindevicesCreated Sep 30, 2021Updated Sep 28, 2026
Labelsfeaturecategory: build/install
System information (version)
  • OpenCV => 4.5.3
  • Operating System / Platform => any major linux distro: CentOS8, fedora34, ubuntu20.04 ...
  • Compiler => clang10
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
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc