Build options for building the python bindings into a venv

I would like to use the python bindings from OpenCV built from source, the latest from Github. I failed finding anything authoritative and recent, but I’m using the following two build flags:

  • PYTHON_EXECUTABLE
  • OPENCV_PYTHON3_INSTALL_PATH ― presumably this is where the final artifacts will go, so I provide my venv’s lib/python3.11/site-packages for this one

I assumed the first of the two would make it use the python executable I’m providing as its value, but in the cmake output it looks like that’s not the case, as it still shows the default system python3 executable there, not the one I’m passing in.

What am I doing wrong for selecting the python executable?
Which additional flags should I be setting for getting the built python bindings used from my venv?

Here’s my best sofar adaptation stitched from various internet posts:

cmake ../opencv -D CMAKE_BUILD_TYPE=Release \
-D BUILD_opencv_python3=ON \
-D PYTHON_VERSION=311 \
-D PYTHON_DEFAULT_EXECUTABLE=$VIRTUAL_ENV/bin/python \
-D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python3.11 \
-D PYTHON3_PACKAGES_PATH=$VIRTUAL_ENV/lib/python3.11/site-packages/ \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.11 \
-D OPENCV_PYTHON3_INSTALL_PATH=$VIRTUAL_ENV/lib/python3.11/site-packages \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$VIRTUAL_ENV/lib/python3.11/site-packages/numpy/core/include \
-D BUILD_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \

And here’s the cmake console output:

Python 3:
--     Interpreter:                 <my venv path>/bin/python3.11 (ver 3.10.13) 
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.10.so (ver 3.10.13)
--     Limited API:                 NO
--     numpy:                       <my venv path>/lib/python3.11/site-packages/numpy/core/include (ver 1.21.5)
--     install path:                <my venv path>/lib/python3.11/site-packages/cv2/python-3.10
-- 
--   Python (for build):            <my venv path>/bin/python

Notably, the Interpreter path shown above is the correct one, but cmake says it is version 3.10.13, whereas it is version 3.11. Some more details are here.

In general, I do not want anything installed globally, as that would avoid using the originally installed OpenCV in other environments.

Thanks!