Skip to content

Add example for NumPy and FindPython. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import nox


hello_list = ["hello-pure", "hello-cpp", "hello-pybind11", "hello-cython"]
hello_list = ["hello-pure", "hello-cpp", "hello-pybind11", "hello-cython", "hello-numpy"]
if not sys.platform.startswith("win"):
hello_list.append("hello-cmake-package")
long_hello_list = hello_list + ["pen2-cython"]
Expand Down
21 changes: 21 additions & 0 deletions projects/hello-numpy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.18...3.22)

project(hello-numpy VERSION "0.1")

# Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
include(GNUInstallDirs)

find_package(Python COMPONENTS Interpreter Development.Module NumPy)

find_package(pybind11 CONFIG REQUIRED)

set(python_module_name _hello)
pybind11_add_module(${python_module_name} MODULE
src/hello/hello_py.cpp
)
target_link_libraries(${python_module_name} PRIVATE Python::NumPy)

install(TARGETS ${python_module_name} DESTINATION .)

# Quiet a warning, since this project is only valid with SKBUILD
set(ignoreMe "${SKBUILD}")
25 changes: 25 additions & 0 deletions projects/hello-numpy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PyBind11 + Scikit Build example


## Building

To build, you must have pip 10 or greater, *or* you need to manually install
`scikit-build` and `cmake`. Once you create a wheel, that wheel can be used in
earlier versions of pip.

Example build and install sequence:

```bash
pip install .
python -c "import hello; hello.hello()"
```

This should print "Hello, World!".

## Testing

Testing is managed by tox. This will build the package in a temp directory and runs the tests in the test dir.

```shell
tox
```
13 changes: 13 additions & 0 deletions projects/hello-numpy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[build-system]
requires = [
"setuptools",
"scikit-build>=0.15",
"cmake",
"ninja",
"pybind11-global",
"oldest-supported-numpy",
]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
testpaths = ["tests"]
14 changes: 14 additions & 0 deletions projects/hello-numpy/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from skbuild import setup

setup(
name="hello-numpy",
version="1.2.3",
description="a minimal example package (with pybind11 and NumPy)",
author='Hameer Abbasi',
license="MIT",
packages=['hello'],
package_dir={'': 'src'},
cmake_install_dir='src/hello',
install_requires=["numpy"],
python_requires='>=3.7',
)
4 changes: 4 additions & 0 deletions projects/hello-numpy/src/hello/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._hello import hello, return_two


__all__ = ("hello", "return_two")
21 changes: 21 additions & 0 deletions projects/hello-numpy/src/hello/hello_py.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <pybind11/pybind11.h>
#include <numpy/arrayobject.h>

namespace py = pybind11;

void hello() {
std::cout << "Hello, World!" << std::endl;
}

py::object zeros2x2() {
return py::reinterpret_steal<py::object>(
PyArray_ZEROS(2, {2, 2}, NPY_FLOAT64, 0);
);
}

PYBIND11_MODULE(_hello, m) {
m.doc() = "_hello";
m.def("hello", &hello, "Prints \"Hello, World!\"");
m.def("zeros2x2", &zeros2x2, "Returns a 2x2 array of zeros.");
}
22 changes: 22 additions & 0 deletions projects/hello-numpy/tests/test_hello_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This kind of import is automatically done when importing hello from outside
import hello
import unittest
import numpy as np


class TestHello(unittest.TestCase):
def test_hello(self):
hello.hello()

def test_return_two(self):
expected = np.zeros((2, 2), dtype=np.float64)
actual = hello.zeros2x2()
self.assertEqual(expected.dtype, actual.dtype)
self.assert_(np.all(expected == actual))


if __name__ == "__main__":
unittest.main()
# You can run all python test with:
# ctest -R python -V
# from the build folder
2 changes: 2 additions & 0 deletions projects/hello-numpy/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testenv]
commands = python -m unittest discover -s tests/