I am trying to import C module created using pybind11 to python script. The directory structure is:
pybind_test:
main.cpp
arithmetic.h
build
CMakeLists.txt
test.py
pybind11 (github repo clone)
It builds successfully and the file module_name.cpython-39-darwin.so
is created. However when running test.py I get:
File "../test.py", line 2, in <module>
from build.module_name import *
ImportError: No module named build.module_name
My CMakeLists file:
cmake_minimum_required(VERSION 3.4...3.18)
set(CMAKE_CXX_STANDARD 17)
project(pybindtest)
add_subdirectory(pybind11)
pybind11_add_module(module_name main.cpp)
How would I import this module into python like a normal python module?
CodePudding user response:
As you noticed, the compiled library must be in a route reachable by the Python executable (which you achieved by moving test.py
to the build directory, where your compiled library is).
You could also move the compiled library to your Python's site-packages
folder, where other modules are stored.