Enabling/disabling Python virtualenv does not result in changing the interpreter
Minimal example:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(foo LANGUAGES NONE)
find_package(Python3 COMPONENTS Interpreter)
message(STATUS "Python ${Python3_EXECUTABLE}")
$ cmake -B build .
-- Found Python3: /usr/bin/python3.10 (found version "3.10.5") found components: Interpreter
-- Python /usr/bin/python3.10
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-test/build
$ python3 -m venv env
$ . env/bin/activate
(env) $ cmake -B build .
-- Python /usr/bin/python3.10
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-test/build
I found this problem when trying to build recent versions of NS-3 which switched from Waf to CMake and offers Python bindings to C++ code.
Here, when virtualenv is activated, Python from virtualenv (env/bin/python3.10
) should be used, but /usr/bin/python3.10
.
The only way I found to make CMake detect it currently is to remove the build
directory and run cmake -B build .
again.
I made a fix !7569 (closed) to FindPython
module that fixes this case.