0

I am working on a Python project that uses a C++ extension module created with Pybind11. The C++ project is built with CMake on Jetbrains CLion, and the generated Python extension file is Binding.cp312-win_amd64.pyd.

I have a Python script (trial.py) that attempts to import the module as follows:

import sys
sys.path.append(r"D:\Binding\cmake-build-debug\Binds")
import Binding_py
print(Binding_py.add(2,3))
print(Binding_py.multiply(3,4)) 

My C++ project's name is Binding. The .pyd file is saves as Binding.cp312-win_amd64.pyd My python project's name is Binds, which resides inside cmake-build-debug of Binding. The .pyd file as well as my py file named as trial.py. are in the same directory 'Binds'.

PYBIND11_MODULE(Binding_py, m) {
    m.doc() = "Example module exposing C++ functions to Python"; 
    m.def("add", &add, "A function that adds two numbers");
    m.def("multiply", &multiply, "A function that multiplies two numbers");
}

When I try to run the above py code, I get the following error -

D:\Binding\cmake-build-debug\Binds\.venv\Scripts\python.exe D:\Binding\cmake-build-debug\Binds\trial.py 
Traceback (most recent call last):
  File "D:\Binding\cmake-build-debug\Binds\trial.py", line 26, in <module>
    import Binding_py
ModuleNotFoundError: No module named 'Binding_py'

I have verified that: The Binding.cp312-win_amd64.pyd file is located in the directory specified in sys.path.append(). I am using the 64-bit version of Python. I have tried rebuilding the project and checking for any missing dependencies, but the issue persists.

What steps can I take to resolve this ModuleNotFoundError and successfully import the Binding_py module?

5
  • your module's name is Binding but you're importing Binding_py? Commented Jan 8 at 8:31
  • I intended the module name to be Binding_py, as seen in the c++ code. But the .pyd file somehow ended up being Binding.(smtg).pyd. I get an error, regardless of which of the two I try to import. Commented Jan 8 at 8:42
  • what about change your file name from Binding.cp312-win_amd64.pyd to Binding_py.cp312-win_amd64.pyd Commented Jan 8 at 9:13
  • There is a relation between the dll name and the module name, they MUST match as python uses the name of the module for both dll lookup and initialization function lookup, you cannot use a suffix in one without the other Commented Jan 8 at 10:57
  • Unfortunately, changing the name of the .pyd file to any one of those recommended, could not resolve the issue. I'll rather try the whole process once again on a later date, and hope for the best... Commented Jan 8 at 12:26

0

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.