3
$\begingroup$

I have a file called cmake_example.cp37-win_amd64.pyd, which I've built using pybind11 (code below). It is located in Python\Python37\Lib\site-packages.

I also have a file called test.py which is simply doing the following:

import cmake_example as m
print(m.add(39,3))
>>> 42

Everything is working just fine when I run this with the python install I have on my system.

I want to use this .pyd-file in blender, but I can't get it to import the module.

import cmake_example as m

gives the following error:

File "\test.py", line 1, in <module>
ImportError: No module named 'cmake_example'
Error: Python script fail, look in the console for now...

I've tried to place the .pyd-file according to the result I get from running

import sys
print(sys.path)

inside blender.

I'm experienced when it comes to both C++ and Blender, but not Python. However, the .pyd-file is doing everything I want outside of Blender, but not inside. Any suggestions on how to successfully import the module?


C++ code:

#include <pybind11/pybind11.h>

int add(int a, int b) {
    return a + b;
}

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    m.doc() = R"pbdoc(
        Pybind11 example plugin
        -----------------------

        .. currentmodule:: cmake_example

        .. autosummary::
           :toctree: _generate

           add
    )pbdoc";

    m.def("add", &add, R"pbdoc(
        Add two numbers

        Some other explanation about the add function.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}
$\endgroup$
0

3 Answers 3

2
$\begingroup$

You need to have the exact same version of Python as used in Blender, and have it (and your module) built with the same compiler. After that, make sure that the module is on sys.path.

$\endgroup$
1
$\begingroup$

Is test.py located at Python\Python37\Lib\site-packages ? If not the test.py import line wont work, resulting in the error you have there.

$\endgroup$
1
$\begingroup$

Your module is named "cmake_example.cp37-win_amd64.pyd", but you try to import cmake_example, so rename it to "cmake_example.pyd"

Having a "." in the module name is not valid, it thinks it should import some method/sub-module of the module.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .