Meson Python build backend

0.19.0 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up a minimal Python project using `meson-python` as the build backend. It involves creating a `pyproject.toml` to declare `mesonpy` as the backend and a `meson.build` file to define the build logic. The example then shows how to build a wheel distribution and optionally install and test it.

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())"

view raw JSON →