A PEP 517 build backend that uses Conan to build Python C/C++ extensions.
Documentation · PyPI · Examples
pip install conan-py-buildNote
The steps below use CMake, but the backend is build-system agnostic — it works with anything Conan can drive. For a Meson version, see basic-meson-pybind11.
- Set
conan-py-buildas your build backend:
[build-system]
requires = ["conan-py-build"]
build-backend = "conan_py_build.build"
[project]
name = "mypackage"
version = "0.1.0"- Add a
conanfile.pywith your C++ dependencies:
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
class MyPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain", "CMakeDeps"
def layout(self):
cmake_layout(self)
def requirements(self):
self.requires("fmt/12.1.0")
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()- Your
CMakeLists.txtmust install the extension into the package directory so it ends up in the wheel:
install(TARGETS _core DESTINATION mypackage)- Build:
pip wheel . -w dist/ -vvvSee the documentation for the full getting started guide, configuration, profiles, dynamic versioning, and more.
See the examples/ directory:
- basic —
fmtextension, recipe viaconanfile-path - basic-pybind11 — pybind11 +
fmt, dynamic version, PEP 639 - basic-meson-pybind11 — pybind11 +
fmtbuilt with Meson instead of CMake - basic-nanobind — nanobind +
fmt,extra-profilefor C++17 - external-sources — pybind11, C++ dep fetched in
source() - cibw-example — pybind11 + cibuildwheel
MIT — see LICENSE.