FindPython older versions

Consider the container created by the dockerfile:

FROM ubuntu:latest
RUN apt update && apt install -y wget gpg python3.8-dev python3.9-dev gcc g++ make
RUN wget -O - 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null && \
 echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] focal main' | tee /etc/apt/sources.list.d/kitware.list >/dev/null && \
 apt-get update && apt-get install -y cmake
COPY CMakeLists.txt .
RUN cmake -S . -B build

with CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
find_package(Python3 3.8 EXACT REQUIRED COMPONENTS Interpreter Development)

i.e., python3.9 and python3.8 are both installed, but I want python 3.8 specifically.

I get the error:

 -- Found Python3: /usr/bin/python3.9 (found version "3.9.5") found components: Interpreter
 CMake Error at sr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
   Could NOT find Python3: Found unsuitable version "3.9.5", but required is
   exact version "3.8" (found /usr/bin/python3.9, found components:
   Interpreter Development Development.Module Development.Embed)
 Call Stack (most recent call first):
   sr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:592 (_FPHSA_FAILURE_MESSAGE)
   sr/share/cmake-3.22/Modules/FindPython/Support.cmake:3166 (find_package_handle_standard_args)
   sr/share/cmake-3.22/Modules/FindPython3.cmake:485 (include)
   CMakeLists.txt:4 (find_package)

Is this the expected behavior?

Do not include(FindPython3).
The Find-Modules are implicitly loaded by the find_package command and shall not be included manually.

The manual include already found the 3.9 version, so the next find_package does not need to search for the executable and just verifies the version.

1 Like