Search code examples
pythoncmake

cmake Python: Cannot use the interpreter


I am using cmake's find_package to locate Python3 like so:

find_package(Python3 REQUIRED COMPONENTS Interpreter)

This works on my system locally but on our CI it fails on some runs (once Linux and once Linux with MXE) it errors with

Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter)

Reason given by package: 

    Interpreter: Cannot use the interpreter "/opt/pyenv/shims/python3.8"

On the same systems it works if I use python3 directly to call my script. Therefore I can say for sure that they do have a working Python environment.

I am wondering what kind of checks find_package performs that lead it to believe that the interpreter was not working. And most importantly: How can I disable these checks?


Note: I can't simply rely on Python being in PATH as this assumption breaks on our Windows CI where the find_package approach works fine.

Note2: I am using the latest cmake version (from Kitware's PPA)


Solution

  • While I still don't know why find_package emitted this particular error, I was able to circumvent it by using a self-written function that locates a Python interpreter and by doing so it prefers executables found in PATH or other standard directories.

    The script is available at https://github.com/Krzmbrzl/FindPythonInterpreter (including usage instructions).

    In short this allowed me to use the following snippet to find a Python 3 interpreter:

    find_python_interpreter(
        VERSION 3
        INTERPRETER_OUT_VAR PYTHON_INTERPRETER
        REQUIRED
    )