FindPython: Prefers system interpreter even if in activated virtualenv through Poetry
I have the following CMakeLists.txt
:
cmake_minimum_required(VERSION 3.28)
project(TestFindPython)
find_package(Python3 COMPONENTS Interpreter)
if (NOT Python3_Interpreter_FOUND)
message(FATAL_ERROR "Python not found at all.")
endif()
message("Python interpreter: ${Python3_EXECUTABLE}")
and a Poetry environment, which is based on the following pyproject.toml
:
[tool.poetry]
name = "test-find-poetry"
version = "0.1.0"
description = ""
authors = ["John Doe"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
[tool.poetry.group.dev.dependencies]
jinja2 = "^3"
When I run the following commands:
poetry install --no-root
poetry run cmake .
I get the output:
Python interpreter: /usr/bin/python3.10
I would expect the output to be:
Python interpreter: /root/.cache/pypoetry/virtualenvs/test-find-poetry-2ezafzvV-py3.10/bin/python3.10
In essence, FindPython
does not detect the binaries from a Poetry virtualenv to be higher priority than the system executor. This results in packages installed through poetry not being available when the python interpreter is invoked.
The same can be observed when using poetry shell
, and in the created shell running cmake .
. Within poetry shell
, one can also confirm that the Poetry python3.10
binary takes priority over the system one, since its directory comes first in $PATH
:
# echo $PATH
/root/.cache/pypoetry/virtualenvs/test-find-poetry-2ezafzvV-py3.10/bin:/root/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin:/root/.local/bin
# which python3.10
/root/.cache/pypoetry/virtualenvs/test-find-poetry-2ezafzvV-py3.10/bin/python3.10
CMake version: 3.28.3 (latest stable as of the time of this issue)