我正在通过pybind 11从c++调用python函数。python函数返回一个数值数组,我想在c++中分析数值数组中的数据。
//the code for testing
#include <pybind11/embed.h> // everything needed for embedding
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
int main()
{
py::scoped_interpreter guard{};
py::print("test python interpreter");
auto sys = py::module::import("sys");
sys.attr("path").attr("append")(
"<path for python module>");
auto hdf5 = py::module::import("reader_hdf5");
auto rdata = hdf5.attr("load_next_chunk")(0, 1);
// how to access the data from the return value ?
py::array_t<int16_t> array(331776);
array = rdata.cast<py::array_t<int16_t>>();
}这里的代码有错误:
terminate called after throwing an instance of 'pybind11::error_already_set'
what(): TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'发布于 2021-01-23 05:53:24
对mutable_data()的调用可以工作
py::array_t<int>rdata= hdf5.attr("load_next_chunk")(0, 1);
int *carray = rdata.mutable_data();然后内容应该存在于carray中。我不是pybind专家,但是这个解决方案是由pybind github forum提供的
https://stackoverflow.com/questions/56759489
复制相似问题