Prashanth Kumar H S
Prashanth Kumar H S

Reputation: 3

How do I construct python code from a give .pyd output in Visual Studio?

I am trying to develop a project to integrate C++ function with python using Pybind11. I am quite familiar with C++, but not so with Python. I have files with following format which I developed for a C++ project.

Output of C++ as: cppproject.pyd

C++ function I want to integrate: int add(int i, int j)

Pybind11 module: PYBIND11_MODULE(example,m){....}

I have all the files I need. But I need to run the add function inside Python now and I am stuck with how to code.

I tried

from cppproject import example
example.add(1, 2)

but it is throwing me an exception as follows:

dynamic module does not define module export function (PyInit_cppproject)

Where am I making mistake with the python code? If it helps, this is my C++ code:

#include <Python.h>
#include <pybind11/pybind11.h>


int add(int i, int j) {
return i + j;
}


 PYBIND11_MODULE(example, m) {

m.def("add", &add, "A function which adds two numbers");

}

The output of this file is in .pyd format to facilitate for python integration. Edit: By the way, I am trying to run both, C++ and Python projects, as one solution in Visual Studio.

Upvotes: 0

Views: 496

Answers (1)

rudimental
rudimental

Reputation: 166

Should work if you name your out-file example.pyd.

then:

from example import add

Alternatively:

PYBIND11_MODULE(cppproject, m) {

auto example = m.def_submodule("example");

...

}

Upvotes: 1

Related Questions