ImportError: No module named with pybind11 on MacOS

I am trying to import C++ module created using pybind11 to python script.
The directory structure is:

pybind_test:

    main.cpp
    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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As you noticed, the compiled library must be in a route (in terms of file path) 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x