Building OpenCV for an alternative python install

I am trying to build opencv 4.5.5 from source for a python 3.10 install on an Ubuntu 20.04 machine.

I have previously built and installed opencv on the same machine for python version 3.8

But when trying to build for python 3.10 the cmake generate step detects a wrong python version.

In cmake-gui these are the variables set for python:

But in the output , shown below, it can be seen that a wrong version number is detected and also the python libraries are not the same as configured.

  Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.18)
    Libraries:                   /usr/lib/x86_64-linux-gnu/ (ver 2.7.18)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)
    install path:                lib/python2.7/dist-packages/cv2/python-2.7

  Python 3:
    Interpreter:                 /usr/local/bin/python3.10 (ver 3.8.10)
    Libraries:                   /usr/lib/x86_64-linux-gnu/ (ver 3.8.10)
    numpy:                       /home/marius/.local/lib/python3.10/site-packages/numpy/core/include (ver 1.19.5)
    install path:                /home/marius/.local/lib/python3.10/site-packages/cv2/python-3.8

  Python (for build):            /usr/bin/python2.7

Any help is greatly appreciated!

I think this issue is quite common as I have seen a few posts like this one. Where people are missing config files for their python version which is a symptom of installing the built opencv install with mismatching python versions

Solved the issue by running cmake inside a pipenv environment with python3.10. Still set the configuration variables to the global python environment. This made cmake detect a python 3.10 version while still building and installing it into the global python3.10 environment.