2
$\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$
  • $\begingroup$ Have you tried appending your system site packages folder to sys.path within blender? sys.path.append("path_to_my_system_site_packages") $\endgroup$ – batFINGER Jun 5 '18 at 14:38
0
$\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$
0
$\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$
0
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.