Use pybind11 with venv

Viewed 40

I have a virtual environment defined using venv and I am trying to build a python module using c++ code and the pybind11 library. The problem is that, when using the virtual environment, python does not see the new module.

This is the code in file main.cpp:

#include <pybind11/pybind11.h>

int myadd(int a, int b) {
      return a + b;
}

namespace py = pybind11;

PYBIND11_MODULE(example, m) {
   m.def("myadd", &myadd);
}

This is the command I use to build the Python module example (virtual environment activated)

c++ -O3 -Wall -shared -std=c++11 -fPIC $(python -m pybind11 --includes) main.cpp -o example.cpython-310-x86_64-linux-gnu.so

When trying to import the module example, Python throws the following error:

>>> import example                                                                         
Traceback (most recent call last):                                                         
  File "<stdin>", line 1, in <module>                                                      
ModuleNotFoundError: No module named 'example' 

On the other hand, without using the virtual environment it all works.

What is wrong? Thanks in advance.

0 Answers
Related