Updating pybind and ImportError: dynamic module
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 670
- Forks
- 219
- Avg merge
- 8d 2h
- Merged PRs (30d)
- 1
Description
my code uses a very old version of pybind: 2.6.0 and everything works fine.
I have two modules, liba and libb
With 2.6.0 I have:
>>> import liba
>>> import libb
>>>
Same code on 2.7.1 and higher, and I get the following error.
I didn't change one single line in the code.
Importing liba still works, but libb fails.
>>> import liba
>>> import libb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_libb)
My module is correctly defined in a cpp file.
PYBIND11_MODULE(libb, m)
My CMake looks like this:
if (BUILD_SHARED_LIBRARY)
set(TARGET_LINK_LIBS liba-shared Eigen3::Eigen)
target_link_libraries(objlib PUBLIC ${TARGET_LINK_LIBS})
find_package(Python REQUIRED COMPONENTS Interpreter Development)
include_directories("${Python_INCLUDE_DIRS}")
add_library(objlib-py OBJECT src/python/libb.cpp)
SET(TARGET_LINK_LIBS_PY Python::Module pybind11::pybind11 ${TARGET_LINK_LIBS})
target_include_directories(objlib-py PUBLIC ${TARGET_INCLUDE_DIRS})
target_link_libraries(objlib-py PUBLIC ${TARGET_LINK_LIBS_PY})
set_property(TARGET objlib-py PROPERTY POSITION_INDEPENDENT_CODE ON)
set(SHARED_LIB ${CMAKE_PROJECT_NAME})
add_library(${SHARED_LIB} SHARED $<TARGET_OBJECTS:objlib> $<TARGET_OBJECTS:objlib-py>)
target_compile_definitions(${SHARED_LIB} PRIVATE VERSION_INFO=${PROJECT_VERSION})
target_include_directories(${SHARED_LIB} PUBLIC ${TARGET_INCLUDE_DIRS})
target_link_libraries(${SHARED_LIB} PUBLIC ${TARGET_LINK_LIBS_PY})
list(APPEND RISK_TARGETS ${SHARED_LIB})
endif (BUILD_SHARED_LIBRARY)
And my environment:
$ python3 --version
Python 3.9.2
$ python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
I compile my libs with gcc12
I am puzzled why upgrading the version of pybind makes my imports fail. Maybe I am doing something wrong, but I can't pinpoint it.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/python/libb.cpp and the CMake configuration shown in the issue, then reproduce the imports with Python 3.9 using pybind11 2.6.0 and 2.7.1 or later. Inspect the built extension and its exported module entry point while comparing the two builds. Done means both liba and libb import successfully after the upgrade, with the relevant build configuration documented or corrected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, python
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100