Pybind11 (global install marker)

3.0.3 · active · verified Wed Apr 15

Pybind11 is a C++ header-only library that provides seamless interoperability between C++ and Python, enabling the creation of Python bindings for C++ code and vice versa. The `pybind11-global` PyPI package, version 3.0.3, typically serves as a marker or a convenient way to ensure the necessary pybind11 C++ headers are discoverable for projects building Python extensions. Pybind11 maintains an active release cadence, with major versions introducing significant features and ABI bumps, followed by patch releases for bug fixes and compatibility updates.

Warnings

Install

Imports

Quickstart

This quickstart provides a `setup.py` script that defines a simple Python extension module written in C++ using pybind11. To run it, save the code as `setup.py` in a directory, then open a terminal in that directory and execute `python setup.py install`. This will compile the C++ code and install the `my_module` package into your Python environment. After installation, you can open a Python interpreter and import `my_module` to use its `add` and `square_list` functions.

import setuptools
import pybind11
import os

# This part makes the quickstart runnable by creating a dummy C++ source file.
# In a real project, this file would exist independently.
cpp_source_code = """
#include <pybind11/pybind11.h>
#include <pybind11/stl.h> // Optional: for STL containers

namespace py = pybind11;

int add(int i, int j) {
    return i + j;
}

std::vector<int> square_list(const std::vector<int>& input) {
    std::vector<int> result;
    for (int x : input) {
        result.push_back(x * x);
    }
    return result;
}

PYBIND11_MODULE(my_module, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    m.def("add", &add, "A function which adds two numbers");
    m.def("square_list", &square_list, "Squares elements of an integer list");
}
"""

cpp_file_name = "my_module.cpp"
if not os.path.exists(cpp_file_name):
    with open(cpp_file_name, "w") as f:
        f.write(cpp_source_code)

# Standard setup.py for a pybind11 extension
setuptools.setup(
    name="my_pybind11_package",
    version="0.0.1",
    author="Checklist.day",
    description="A minimal pybind11 example buildable via setup.py",
    ext_modules=[
        setuptools.Extension(
            "my_module", # Name of the Python module
            [cpp_file_name],
            include_dirs=[pybind11.get_include()], # Get pybind11 headers
            language='c++',
            extra_compile_args=["-O3", "-Wall", "-std=c++17"],
        ),
    ],
    zip_safe=False,
    python_requires=">=3.8",
)

print(f"To build and install: python {os.path.basename(__file__)} install")
print("Then, in Python: import my_module; print(my_module.add(1, 2)); print(my_module.square_list([1,2,3]))")

view raw JSON →