Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. C++ library python binding generation with Shiboken
QtWS25 Last Chance

C++ library python binding generation with Shiboken

Scheduled Pinned Locked Moved Language Bindings
6 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kepz
    wrote on last edited by
    #1

    Hi, I followed the tutorial up on the wiki http://qt-project.org/wiki/PySide_Binding_Generation_Tutorial
    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
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    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())
            PyErr_Print();
    /// -----------
    
    ///Our python file to interpret
    const char* filename = "/Users/alexandre/Downloads/BindingTest/FooLibTest/foolib_test.py";
    FILE* file = fopen&#40;filename,"r"&#41;;
    PyRun_SimpleFile&#40;file,filename&#41;;
    
    ///close python
    Py_Finalize();
    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/foolib_test.py", 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.

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      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.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kepz
        wrote on last edited by
        #3

        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;)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kepz
            wrote on last edited by
            #5

            how should I contact them ?;)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved