Please nominate your Qt Champions for 2021!

C++ library python binding generation with Shiboken

  • Hi, I followed the tutorial up on the wiki
    but I can't get it to work properly. I'm on MacOSX

    So far here's what I did:

    • Build FooLib (static) ---> libFooLib.a
    • Create the typesystem_foo.xml
    • Run shiboken with the following command:

    @shiboken-2.7 global.h --include-paths=.:/opt/local/include/PySide-2.7:/opt/local/include --typesystem-paths=/opt/local/share/PySide-2.7/typesystems --output-directory=../FooLibBinding typesystem_foo.xml@

    • Build the FooLibBinding dynamic library from the resulted generated c++ code --> libFooLibBinding.dylib

    Now instead of just running python interpreter from the command line, I made a C++ program which would load the python interpreter and open a .py script using the FooLib. This program links dynamically against libFooLibBinding.dylib so I guess all the symbols needed for the foolib module to work are there;)

    here's the code:

    #include <iostream>
    #include <Python.h>

    int main(int argc, char* argv[])

    ///Python init
    PySys_SetArgv(argc, argv); /// relative module import
    ///Try out loading the module, this is just for testing
    /// -----------
    PyObject *sysPath = PySys_GetObject("path");
    PyObject *path = PyString_FromString("/Users/alexandre/Downloads/BindingTest");
    int result = PyList_Insert(sysPath, 0, path);
    PyObject *pModule = PyImport_ImportModule("foolib");
    if (PyErr_Occurred())
    /// -----------
    ///Our python file to interpret
    const char* filename = "/Users/alexandre/Downloads/BindingTest/FooLibTest/";
    FILE* file = fopen&#40;filename,"r"&#41;;
    ///close python
    return 0;


    When running the program, it fails when trying to load the module a first time saying:
    @ImportError: No module named foolib@

    And then a second time when running the .py script :

    @Traceback (most recent call last):
    File "/Users/alexandre/Downloads/BindingTest/FooLibTest/", line 1, in <module>
    from foolib import FooClass
    ImportError: No module named foolib@

    So obviously it cannot find the module generated from the bindings. My question is what should I do so it can find it ?

    The tutorial uses a Makefile but doesn't seem to do much more than just linking the binding dynamic library.

  • Can you actually import the module from the command line? If so, there is probably an issue with the Python environment you are constructing, if not then there is an issue with the dylib you are creating.

  • Hi,

    I actually solved my problem because on MacOSX somehow the library has to end with .so extension and have exactly the same name as the python module;)

  • Lifetime Qt Champion


    You should maybe send a message to the Shiboken authors so that they add a note about that in their documentation, that might help future users

  • how should I contact them ?;)

  • Lifetime Qt Champion

    You can try the pyside IRC channel or mailing list :)

Log in to reply