https://github.com/ssciwr/pybind11-numpy-example
Example of using pybind11 with numpy and publishing to PyPI and conda-forge
https://github.com/ssciwr/pybind11-numpy-example
cibuildwheel conda-forge continuous-deployment continuous-integration cpp pybind11 pypi python wheels
Last synced: about 14 hours ago
JSON representation
Example of using pybind11 with numpy and publishing to PyPI and conda-forge
- Host: GitHub
- URL: https://github.com/ssciwr/pybind11-numpy-example
- Owner: ssciwr
- License: mit
- Created: 2021-06-18T18:09:07.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-07T17:24:45.000Z (24 days ago)
- Last Synced: 2025-04-30T22:46:27.906Z (about 14 hours ago)
- Topics: cibuildwheel, conda-forge, continuous-deployment, continuous-integration, cpp, pybind11, pypi, python, wheels
- Language: Python
- Homepage:
- Size: 313 KB
- Stars: 25
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# pybind11-numpy-example
[](https://opensource.org/licenses/MIT)
[](https://pypi.org/project/pybind11-numpy-example)
[](https://anaconda.org/conda-forge/pybind11-numpy-example)
[](https://pypi.org/project/pybind11-numpy-example)
[](https://github.com/lkeegan/pybind11-numpy-example/actions/workflows/ci.yml)
[](https://pybind11-numpy-example.readthedocs.io/)# What
A simple example of how to use [pybind11](https://github.com/pybind/pybind11) with [numpy](https://numpy.org/) and publish this as a library on [PyPI](https://pypi.org/project/pybind11-numpy-example/) and [conda-forge](https://anaconda.org/conda-forge/pybind11-numpy-example).
This C++/Python library creates a `std::vector` of 16-bit ints,
and provides a Python interface to the contents of this vector in a few different ways:- a Python [List](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) (copy the data)
- a NumPy [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) (copy the data).
- a NumPy [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) (move the data).# Why
Python Lists are great!
However, when storing many small elements of the same type,
a Numpy array is much faster and uses a lot less memory:

# How
The pybind11 code is in [src/pybind11_numpy_example_python.cpp](https://github.com/ssciwr/pybind11-numpy-example/blob/main/src/pybind11_numpy_example_python.cpp).
The python package is defined in [pyproject.toml](https://github.com/ssciwr/pybind11-numpy-example/blob/main/pyproject.toml)
and uses [scikit-build-core](https://github.com/scikit-build/scikit-build-core).Each tagged commit triggers a [GitHub action job](https://github.com/ssciwr/pybind11-numpy-example/actions/workflows/pypi.yml)
which uses [cibuildwheel](https://cibuildwheel.readthedocs.io/) to build and upload a new release including binary wheels for all platforms to [PyPI](https://pypi.org/project/pybind11-numpy-example/).The [conda-forge package](https://anaconda.org/conda-forge/pybind11-numpy-example) is generated from [this recipe](https://github.com/conda-forge/pybind11-numpy-example-feedstock/blob/main/recipe/meta.yaml), and automatically updates when a new version is uploaded to PyPI.
The scripts used to generate the above plots are in [scripts](https://github.com/ssciwr/pybind11-numpy-example/tree/main/scripts).
This repo was quickly set up using the SSC [C++ Project Cookiecutter](https://github.com/ssciwr/cookiecutter-cpp-project).