How do I import a .pyd module written in C++ to Python using PyBind11

Question:

I am working on Windows 10 with Python 3.9.7 and have anaconda setup on my laptop.
I have compiled a C++ code calcSim.cpp where the module name is calJaccSimm and am able to successfully generate a .pyd file with the following extension .cp39-win_amd64.pyd as described here .

I am launching my jupyter notebook at the following location: jupyter-notebook D:projectssem4code and my .pyd file named calcSim.cp39-win_amd64.pyd is at the same location.

When trying to import module using:

import calJaccSimm I am getting ModuleNotFoundError.

I have tried the following things:

  1. import sys

    sys.path.insert(0, 'D:projectssem4code')

  2. import os

    os.dll_directory("D:projectssem4code")

  3. Setup environment variables with the path ‘"D:projectssem4code"’

  4. Tried putting the generated .pyd in different locations like anacondaDLLs and
    anacondalibsite-packages folder.

But after all this, I am still not able to load the module. Please help.

Edit 1: I had multiple versions of python on my machine. I deleted all the versions and re-installed anaconda as well. Still facing the same issue.

Asked By: tushaR

||

Answers:

There are three places where the module name needs to be consistent: When calling the PYBIND11_MODULE macro, in the file name of the extension, and in the import statement inside python.

It seems that your pyd file is inconsistently named calcSim instead of calcJaccSimm.

Answered By: unddoch