Meson Python build backend
meson-python is a Python build backend (PEP 517) that integrates the Meson build system for Python packages. It is particularly well-suited for projects that include extension modules written in compiled languages such as C, C++, Cython, Fortran, Pythran, or Rust. The library is actively maintained, with version 0.19.0 currently available, and new releases occurring every few months to introduce features and address compatibility.
Warnings
- breaking Python 3.7 support was dropped in meson-python 0.18.0, and Python 3.8 support was dropped in 0.19.0. Projects using these older Python versions will need to upgrade to at least Python 3.9.
- breaking The minimum required Meson version was bumped to 0.64.0 in meson-python 0.18.0, and Meson 1.2.3 or later is required for Python 3.12+ in meson-python 0.15.0.
- gotcha Meson's `install_data()` function installs non-package data files to a platform-specific location, making them difficult to reliably find at runtime after wheel installation.
- gotcha Passing native build options to Meson via `pip` or `build` can be complex due to the layers of abstraction introduced by the PEP 517 backend. Full control over Meson flags might be hindered.
- gotcha Changes in `os.path.isabs` semantics in Python 3.13 have been reported to break Meson's path detection on Windows, which could lead to build failures for projects using `meson-python`.
Install
-
pip install meson-python -
pip install build meson ninja
Imports
- mesonpy
# Defined in pyproject.toml [build-system] build-backend = 'mesonpy'
Quickstart
mkdir my_project
cd my_project
# Create pyproject.toml
cat <<EOL > pyproject.toml
[build-system]
requires = ["meson-python", "meson", "ninja"]
build-backend = "mesonpy"
[project]
name = "my_example_package"
version = "0.1.0"
EOL
# Create meson.build
cat <<EOL > meson.build
project('my_example_package', 'python', version: '0.1.0')
python = import('python').find_installation(pure: false)
python.install_sources(
'src/my_example_package/__init__.py',
subdir: 'my_example_package'
)
EOL
# Create a simple Python source file
mkdir -p src/my_example_package
cat <<EOL > src/my_example_package/__init__.py
def greet():
return "Hello from my_example_package!"
EOL
# Build the package (e.g., a wheel)
python -m build --wheel
# Install and test (optional)
pip install dist/*.whl
python -c "from my_example_package import greet; print(greet())"