2

I try to create a simple C++ module for python with Boost, but python gives me ModuleNotFoundError: No module named 'MyLib'.
The .py file is in the same location as MyLib.dll.

UPD: if i change dll to pyd or replace add_library(MyLib MODULE MyLib.cpp) with PYTHON_ADD_MODULE(MyLib MyLib.cpp) I get another error: ImportError: DLL load failed while importing MyLib: The specified module could not be found.

CMake

set(Boost_NO_SYSTEM_PATHS TRUE)
set(BOOST_ROOT "C:/local/boost_1_80_0")
set(CMAKE_SHARED_MODULE_PREFIX "")

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})

find_package(Boost COMPONENTS python310 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

add_library(MyLib MODULE MyLib.cpp)

target_link_libraries(MyLib ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

C++

#include <boost/python.hpp>

    auto* get()
    {
        return "Hello from C++";
    }
    
    BOOST_PYTHON_MODULE(MyLib)
    {
        using namespace boost::python;
        def("get", get);
    }

Python

from MyLib import get
get()
8
  • Are you in the same directory as your C++ library? Otherwise Python doesn't know where to find it. Commented Nov 2, 2022 at 16:36
  • Yes, .py file with .dll is in the same directory Commented Nov 2, 2022 at 16:39
  • 1
    if i change dll to pyd I get another error: ImportError: DLL load failed while importing MyLib: The specified module could not be found. Commented Nov 2, 2022 at 17:43
  • 1
    Then use something like github.com/lucasg/Dependencies to inspect which third party DLLs your module depends on, and make sure they're in the search path. You're probably linking with boost.python dynamically, so that is likely the culprit. Commented Nov 2, 2022 at 18:06
  • 1
    Yes, it seems you are right, I copied boost_python310 dll to my output path and it's working now Commented Nov 2, 2022 at 18:53

1 Answer 1

1

You mention that your binary module is named MyLib.dll. That is the first problem.[1], [2]

On Windows, CPython expects binary modules to have extension .pyd.

Either rename the DLL manually, or (as you mention) use PYTHON_ADD_MODULE instead of add_library to achieve the same automatically.

Once you do this, another problem may appear:

ImportError: DLL load failed while importing MyLib: The specified module could not be found.

When you're unsure, the best way to debug such errors on Windows is to use a tool like https://github.com/lucasg/Dependencies to find which third party libraries are required, and possibly missing.

In this case, the likely culprit will be the boost.python DLL. Generally boost.python is linked dynamically, since that allows multiple binary modules that depend on it to work together. Make sure that DLL is in the library search path and things should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.