admin管理员组

文章数量:1123602

I am trying to bind c++ program with python using pybind11. The c++ program includes two objects I want to use in my python program, as follows:

#include <pybind11/pybind11.h>

#include "optixSphere.h"
#include "common.h"

namespace py = pybind11;

PYBIND11_MODULE(rts, m) {
    py::class_<rts::KNN>(m, "KNN")
        .def(py::init<>())  // Default constructor
        .def_readwrite("num", &rts::KNN::num)
        .def_readwrite("dist_list", &rts::KNN::dist_list)
        .def_readwrite("knn_list", &rts::KNN::knn_list);

    py::class_<rts::RTSphere>(m, "RTSphere")
        .def(py::init<const std::vector<point_t>&, int, float, int>())
        .def("Query", &rts::RTSphere::Query, py::arg("queries"), py::arg("query_num"),
             py::arg("result"))
        .def("DisplayMetrics", &rts::RTSphere::DisplayMetrics);
}

In CMakeLists.txt, the corresponding commands are:

### ===== For pybind11
pybind11_add_module(rts
    bindings.cpp
    ${embedded_ptx_code} 
    optixSphere.cpp
    cuda_helper.cu
)

# Install the module to Python site-packages
message(STATUS "Python3_SITEARCH: ${Python3_SITEARCH}")
install(TARGETS rts DESTINATION ${Python3_SITEARCH}/rts)

After installing the module, I used it in my python code:

import numpy as np
from scipy.spatial import KDTree
import random
import rts

data_path = "data/test_point.csv"
device_id = 3
radius = 20.0

header = np.genfromtxt(data_path, delimiter=',', max_rows=1, dtype=int)
point_num = header[0]
point_dim = header[1]

print(f"num = {point_num}, dim = {point_dim}")

data = np.genfromtxt(data_path, delimiter=',', skip_header=1).tolist()
print(data[0:10])

bvh = rts.RTSphere(data, point_num, radius, device_id)

The error is:

Traceback (most recent call last):
  File "/home/xzx/Project/RTCC/example/rt_search.py", line 22, in <module>
    bvh = rts.RTSphere(data, point_num, radius, device_id)
          ^^^^^^^^^^^^
AttributeError: module 'rts' has no attribute 'RTSphere'

What is the reason leading to this problem?

I have trid to move the python program into the directory where the module locates, the error was gone.

本文标签: pythonModule has not attribute in pybind11Stack Overflow