Pybind11 (global install marker)
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
- breaking Pybind11 3.0.0 introduced a significant ABI (Application Binary Interface) bump. This means that C++ extensions compiled with older versions of pybind11 (e.g., 2.x) are not binary compatible with pybind11 3.x.
- gotcha Pybind11 is fundamentally a C++ header-only library for creating Python bindings. Its core functionality is written in C++ and compiled into a shared library (e.g., .so, .pyd). The Python package (e.g., `pybind11-global` or `pybind11`) primarily provides the C++ headers and Python-side build-time utilities (like `pybind11.get_include()`) for tools like `setuptools` or `CMake`, not direct Python runtime imports for binding features.
- gotcha The `pybind11-global` package is often a marker or a dependency wrapper. The actual Python utility functions (e.g., `pybind11.get_include()`) that help with the build process are provided by the `pybind11` package, which is a dependency of `pybind11-global`. While installing `pybind11-global` will bring in `pybind11`, remember that the Python-side helpers are prefixed with `pybind11.`
- breaking Pybind11 3.0.0 dropped official support for older Python versions. Specifically, Python 3.6 and 3.7 are no longer supported.
- gotcha Pybind11 requires a C++11 compatible compiler at a minimum. For full feature support and modern C++ best practices, using a compiler that supports C++17 or C++20 is often recommended.
Install
-
pip install pybind11-global
Imports
- get_include
import pybind11 # ... then use pybind11.get_include() in your setup.py
Quickstart
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]))")