Unable to import module from the shared .pyd built with CMake #5141
Replies: 1 comment 1 reply
-
have the same problem! I appreciate all help! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. I'm trying to build a simple working example of c++/python binding. I'm working on Windows 11. I installed pybind11 just executing
pip install pybind11
. I have Python 3.12.3, only this versionThis is the c++ source (called testing.cpp), just the classical add function
-----------------------------------testing.cpp------------------------------------------------------
`#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(testing, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
}
----------------------------------testing.cpp--------------------------------------------------------- This source is located at
.../Codeprojects/C++_gcc/test`.I use VS Code on g++ x64 environment. The CMake building configurations are the following
---------------------CMakeLists.txt------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.12.0)
set (CMAKE_CXX_STANDARD 20)
#I had problems using add_subdirectory(pybind11)
set(CMAKE_PREFIX_PATH C: ...\Python\Python312\Lib\site-packages\pybind11\share\cmake)
find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)
pybind11_add_module(testing testing.cpp)
---------------------CMakeLists.txt-----------------------------------------------------------------------
I also use VS Code for building with CMake.
the CMake .txt file is also at
C: .../Codeprojects/C++_gcc/test
. It builds without problem, generating a folder called build with the filetesting.cp312-win_amd64.pyd
within this folder. So this .pyd file is located atC: .../Codeprojects/C++_gcc/test/build
My test.py is located at
C: .../Codeprojects/Python
, it is as follows---------------------------------test.py--------------------------------------------------------------
import sys
sys.path.append("C: .../Codeprojects/C++_gcc/test/build")
import testing
----------------------------------test.py-------------------------------------------------------------
I have always the same problem: Pylance warns Import "testing" could not be resolved . The execution fails with ImportError: DLL load failed while importing testing: The specified module could not be found. I don't think this is dependency problem since I'm not using anything beyond core C++. Could it be conflicting Python versions (pybind11 is expecting other version)? Maybe something in the CMake file (I'm also new to CMake). I appreciate all help.
Beta Was this translation helpful? Give feedback.
All reactions