find_package(Python) fails with "standalone" python

I am running into issues with finding Python in combination with a “standalone” (static) python. This is a minimal CMakeLists.txt I use for testing:

cmake_minimum_required(VERSION 3.16)
project(foo)

find_package(Python REQUIRED COMPONENTS Development)

In my case I am trying to discover the python unzipped from this archive. The directory structure of the extracted archive is fairly standard. Here is an (abbreviated) directory structure:

path/to/unzipped/archive
└── python
    ├── bin
    │   ├── python3 -> python3.11
    │   ├── python3.11
    │   ├── ...
    ├── include
    │   └── python3.11
    │       ├── Python.h
    │       └── ...
    ├── lib
    │   ├── libpython3.11.so -> libpython3.11.so.1.0
    │   ├── libpython3.11.so.1.0
    │   ├── libpython3.so
    │   ├── ...
    └── ...

I have tried various way to give cmake a hint where to find Python:

  • specifying `-D Python3_ROOT_DIR=path/to/unzipped/archive/python
  • adding path/to/unzipped/archive/python/bin to PATH
  • specifying
    • Python3_EXECUTABLE=path/to/unzipped/archive/python/bin/python3,
    • Python3_LIBRARY=path/to/unzipped/archive/python/lib/libpython3.so
    • Python3_INCLUDE_DIR=path/to/unzipped/archive/python/include

but without success. I always get this error:

CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Python (missing: Python_INCLUDE_DIRS Python_LIBRARIES
  Development Development.Module Development.Embed)
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindPython.cmake:561 (find_package_handle_standard_args)
  CMakeLists.txt:4 (find_package)

I can’t really think of a reason why its failing to discover it

My environment:

  • cmake version 3.22.1
  • Linux jammy 6.2.0-32-generic #32~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 18 10:40:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

You have to use a consistent name between the hints prefix and the package.

Use Python_ prefix for the hints if you want to use Python package. Or use Python3 package with the hints you have defined.

And be aware that if only Development component is specified, the python3 interpreter is not searched, so that explain why you fail to found the artifacts by adding the python bin path to PATH environment variable.

Ah yes, stupid mistake by me. I guess I got a bit confused looking at both the FindPython and FindPython3 documentation in parallel.

Got it to work now by also adding the Interpreter component:

cmake_minimum_required(VERSION 3.16)
project(foo)

find_package(Python REQUIRED COMPONENTS Interpreter Development)

and then just providing the hint for the python root directory:

cmake \
-G "Unix Makefiles" \
-B build \
-D Python_ROOT_DIR=/path/to/unzipped/archive/python

What maybe also played a role is that I didn’t clear the cmake cache between my many attempts, playing with FindPython and FindPython3 and cmake then doesn’t reattempt to find a certain variable if it already found it before, which lead to surprising behavior (like I removed all my defines but it still knew where to find some of the python components). lesson learned, clear the cache.

(edit: also the interpreter component needs to be added in the find command)