Pybind11: ImportError: dynamic module does not define init function (PyInit_sky)

Created on 11 Apr 2016  路  5Comments  路  Source: pybind/pybind11

Hi,
I am trying to use pybind11 in my project. First I build all object files then I link them into single shared library. The file is called sky.so. All works fine but when I try to import my module - "import sky" I get error placed in the title - ImportError: dynamic module does not define init function (PyInit_sky)

C++ binding file looks like this -

#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

namespace py = pybind11;

PYBIND11_PLUGIN(sky) {
    py::module m("sky", "pybind11 example plugin");

    m.def("add", &add, "A function which adds two numbers");

    return m.ptr();
}

Compiling - g++ -std=c++14 -DFORMAT_STRING_EXCEPT -Ilibs -Isrc -I/usr/include/jsoncpp -Ilibs/pybind11/include -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -Wall -Wextra -Wformat=2 -Wcast-qual -Wpointer-arith -Wstrict-aliasing -pedantic -mtune=native -march=native -fpic -Wformat -Werror=format-security -c -g3 -O0 src/py_binding/data_binding.cpp -o build/d/o/data_binding.o

And linking is done using command - g++python-config --ldflags-shared build/d/o/data.o build/d/o/json.o build/d/o/profile.o build/d/o/query.o build/d/o/data_binding.o -lboost_date_time -lboost_filesystem -lboost_system -ljsoncpppython-config --libs-o build/d/sky.so

Am I doing it wrong or something or is there some bug in the library?

Most helpful comment

aha, I see -- two possibilities: there seems to be some mismatch between the filename and the module name declared above (probably not, based on what you wrote).
possibility 2: the Python version found by python-config is not the one the same one that you're using the test the plugin.

All 5 comments

.. and what is the error?

aha, I see -- two possibilities: there seems to be some mismatch between the filename and the module name declared above (probably not, based on what you wrote).
possibility 2: the Python version found by python-config is not the one the same one that you're using the test the plugin.

Edited ^. I get error while importing my module in python. It is python3 if that is of any importance.

I see. That was probably the problem. Thank you.

Hello,
I run into the same issue while trying to go trough the example in README.md.
After reading this issue and experimenting I found the issue was caused by running the idlex shell instead of idle3 (I am using python 3.5).
Switching to idle3 resolved the problem. I am posting this to maybe help others running into the same problem.

Was this page helpful?
0 / 5 - 0 ratings