CMake (Python distribution)
CMake is an open-source, cross-platform family of tools designed to build, test, and package software. The `cmake` Python package provides Python wheels for the CMake executable, making it easy to install and use CMake within Python environments. It is currently at version 4.3.1 and typically releases new versions in sync with upstream CMake releases.
Warnings
- breaking Python 3.7 and older `manylinux` wheels were dropped in CMake Python distribution version 4.1.0. If you require Python 3.7 or older `manylinux` environments (e.g., `manylinux2010`, `manylinux1`), you must use an older version of the `cmake` package (e.g., 4.0.3 for Python 3.7/manylinux2010, 3.22.x for manylinux1).
- gotcha The `cmake` Python package primarily serves to provide a convenient, platform-specific distribution of the CMake *executable*. It does not offer a high-level Python API for interacting with CMake's build system features directly within Python. For building Python extension modules using CMake, consider higher-level build backends like `scikit-build` or `scikit-build-core` which utilize this `cmake` package.
- deprecated Older CMake modules `FindPythonInterp` and `FindPythonLibs` are deprecated and are no-ops since CMake 3.27. Using them in `CMakeLists.txt` will not correctly find Python installations with modern CMake versions.
- breaking CMake 3.28 (and subsequent versions distributed by this package) changed the `PATH` behavior for Windows, where `find_{library,path,file}()` no longer searches the system `PATH` by default. This might break existing projects that rely on `PATH` for finding libraries on Windows.
- breaking CMake 3.5 compatibility was removed. Calls to `cmake_minimum_required()` or `cmake_policy()` setting policy versions below 3.5 will now result in an error.
Install
-
pip install cmake
Imports
- cmake_path
import cmake cmake_executable = cmake.cmake_path()
- cmake_version
import cmake version_str = cmake.cmake_version()
Quickstart
import cmake
import subprocess
import sys
try:
cmake_executable = cmake.cmake_path()
print(f"Found CMake executable at: {cmake_executable}")
# Run a simple CMake command, e.g., --version
result = subprocess.run([cmake_executable, '--version'], capture_output=True, text=True, check=True)
print("\nCMake --version output:")
print(result.stdout)
# Example of getting CMake version programmatically
print(f"\nCMake version from package: {cmake.cmake_version()}")
except subprocess.CalledProcessError as e:
print(f"Error running CMake: {e}", file=sys.stderr)
print(f"Stderr: {e.stderr}", file=sys.stderr)
except Exception as e:
print(f"An unexpected error occurred: {e}", file=sys.stderr)