mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
1.2 KiB
1.2 KiB
numpy
How to run numpy module programs with gsoc-2024-dev pybind11
- Prepare the python code file with the numpy operations you want to run.
For example : let's try out numpy arange function intest_numpy.py
import numpy_bindings as np
def test_arange(n):
a = np.arange(n)
print(a.sum())
test_arange(100)
- Read the script and execute it in
test_numpy.cpp
.
#include <pybind11/embed.h>
#include <fstream>
#include <sstream>
#include <string>
namespace py = pybind11;
using namespace pybind11;
int main() {
py::scoped_interpreter guard{};
std::ifstream file("test_numpy.py");
std::stringstream buffer;
buffer << file.rdbuf();
std::string script = buffer.str();
py::exec(script);
return 0;
}
- Build the project at root to generate the executable at
build/gsoc2024
.
cmake -B build
cmake --build build
- Now run the executable to get the output.
|base| gsoc-2024-dev ±|main ✗|→ build/gsoc2024
4950