How it works

find_package is a wrapper command for CMake modules written for discovering and setting up packages. These modules contain CMake commands to identify packages in standard locations on the system. The files for the CMake modules are called Find<name>.cmake and the commands they contain will be run internally when a call to find_package(<name>) is issued.

In addition to actually discovering the requested package on your system, find modules also set up a handful of useful variables, reflecting what was actually found, which you can use in your own CMakeLists.txt. In the case of the Python interpreter, the relevant module is FindPythonInterp.cmake, which is shipped with CMake, and sets the following variables:

  • PYTHONINTERP_FOUND, a Boolean signaling whether the interpreter was found
  • PYTHON_EXECUTABLE, the path to the executable for the Python interpreter
  • PYTHON_VERSION_STRING, the full version of the Python interpreter
  • PYTHON_VERSION_MAJOR, the major version of the Python interpreter
  • PYTHON_VERSION_MINOR, the minor version of the Python interpreter
  • PYTHON_VERSION_PATCH, the patch number of the Python interpreter

It is possible to force CMake to look for specific versions of a package. For example, use this to request any version of the Python interpreter greater or equal to 2.7:

find_package(PythonInterp 2.7)

It is also possible to enforce that dependencies are satisfied:

find_package(PythonInterp REQUIRED)

In this case, CMake will abort configuration if no suitable executable for the Python interpreter is found in the usual lookup locations.

CMake has modules for finding many widespread software packages. We recommend to always search the CMake online documentation for existing Find<package>.cmake modules and to read their documentation before using them. The documentation for the find_package command can be found at https://cmake.org/cmake/help/v3.5/command/find_package.html. A good alternative to online documentation is to browse CMake module sources in  https://github.com/Kitware/CMake/tree/master/Modules - their headers document the variables that a module uses, as well as the variables set by the module that can be used in your CMakeLists.txt.