Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Embedding Python does not throw an error, but the program crashes anyways
Qt 6.12 LTS is out! See what's new in the release blog

Embedding Python does not throw an error, but the program crashes anyways

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 5.6k Views 1 Watching
  • 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.
  • L lfreeman6490

    @eyllanesc There is no difference in the output when entering that line. I cleaned, reran qmake, rebuilt, then ran directly from the executable as well as from qtcreator. The output just says

    The program has finished unexpectedly
    
    eyllanescE Offline
    eyllanescE Offline
    eyllanesc
    wrote on last edited by
    #8

    @lfreeman6490 I never said that was the solution, do not rush. I'm just asking for some debugging information to analyze the cause of the error.

    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

    L 1 Reply Last reply
    1
    • eyllanescE eyllanesc

      @lfreeman6490 I never said that was the solution, do not rush. I'm just asking for some debugging information to analyze the cause of the error.

      L Offline
      L Offline
      lfreeman6490
      wrote on last edited by
      #9

      @eyllanesc Okay, thanks. I just wanted to clearly show what the error message was and what I had done already

      eyllanescE 1 Reply Last reply
      0
      • L lfreeman6490

        @eyllanesc Okay, thanks. I just wanted to clearly show what the error message was and what I had done already

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #10

        @lfreeman6490 Comment on the line that generates the error and tell me what is printed on the console

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        L 1 Reply Last reply
        1
        • eyllanescE eyllanesc

          @lfreeman6490 Comment on the line that generates the error and tell me what is printed on the console

          L Offline
          L Offline
          lfreeman6490
          wrote on last edited by
          #11

          @eyllanesc Okay, commenting out that line gives me hex code errors

          0x67b0128 0x0 0x57290610 
          
          eyllanescE 1 Reply Last reply
          0
          • L lfreeman6490

            @eyllanesc Okay, commenting out that line gives me hex code errors

            0x67b0128 0x0 0x57290610 
            
            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by eyllanesc
            #12

            @lfreeman6490 From the print we can see that pModule is null, so you get this behavior.

            Change to:

            if(!(pModule = PyImport_Import(pName))){
                PyErr_Print();
                return;
            }
            

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            L 1 Reply Last reply
            1
            • eyllanescE eyllanesc

              @lfreeman6490 From the print we can see that pModule is null, so you get this behavior.

              Change to:

              if(!(pModule = PyImport_Import(pName))){
                  PyErr_Print();
                  return;
              }
              
              L Offline
              L Offline
              lfreeman6490
              wrote on last edited by lfreeman6490
              #13

              @eyllanesc Where in the code should I add that? Should that be directly after creating pModule? Or at the very last spot in the code?

              eyllanescE 1 Reply Last reply
              0
              • L lfreeman6490

                @eyllanesc Where in the code should I add that? Should that be directly after creating pModule? Or at the very last spot in the code?

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #14

                @lfreeman6490 Change pModule = PyImport_Import(pName);

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                L 1 Reply Last reply
                0
                • eyllanescE eyllanesc

                  @lfreeman6490 Change pModule = PyImport_Import(pName);

                  L Offline
                  L Offline
                  lfreeman6490
                  wrote on last edited by
                  #15

                  @eyllanesc Okay so by changing that it is no longer crashing, but it is not calling my function. The function is a simple test function that should return a number

                  def test_function():
                      return 7
                  
                  eyllanescE 1 Reply Last reply
                  0
                  • L lfreeman6490

                    @eyllanesc Okay so by changing that it is no longer crashing, but it is not calling my function. The function is a simple test function that should return a number

                    def test_function():
                        return 7
                    
                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #16

                    @lfreeman6490 It seems that you have not understood the problem. Your code has not imported the module so pModule is null.

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    3
                    • L lfreeman6490

                      I am trying to embed a python interpreter into my c++ gui. I am including Python.h and then in my function

                      void pl::on_pb_connect_disconnect_clicked()
                      {
                          PyObject* pName, *pModule, *pDict, *pFunc;
                      
                          Py_Initialize();
                      
                          std::string filename = "test_python_file.py";
                          const char* cstr1 = filename.c_str();
                      
                          pName = PyUnicode_FromString(cstr1);
                      
                          pModule = PyImport_Import(pName);
                      
                          pDict = PyModule_GetDict(pModule);
                      
                          std::string function_name = "test_function";
                          const char* cstr = function_name.c_str();
                      
                          pFunc = PyDict_GetItemString(pDict, cstr);
                      
                          if (PyCallable_Check(pFunc))
                          {
                              PyObject_CallObject(pFunc, NULL);
                          }
                          else
                          {
                              PyErr_Print();
                          }
                      
                          Py_Finalize();
                      }
                      

                      The basic idea is that once the 'connect' button is hit, it calls test_function in test_python_file.py. It builds and runs successfully, once it is launched and I hit the 'connect' button, the program crashes. I have cleaned, rebuild, ran qmake, etc. Running in debug mode gives me a division by 0 error. If I comment out the code line by line I see that the errors comes from

                      pDict = PyModule_GetDict(pModule);
                      

                      Once that line is used again, the program crashes.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #17

                      @lfreeman6490 said in Embedding Python does not throw an error, but the program crashes anyways:

                      std::string filename = "test_python_file.py";
                      const char* cstr1 = filename.c_str();
                      
                      pName = PyUnicode_FromString(cstr1);
                      
                      if(!(pModule = PyImport_Import(pName))){
                          PyErr_Print();
                          return;
                      }
                      

                      From the 0x0 output you really should be able to understand that PyImport_Import(pName) returned nullptr. That means your test_python_file.py could not be loaded, Either the file could not be found, or there is some problem loading it. Hopefully the PyErr_Print(); gave some clue as to why, but you never said anything about what it output.

                      Start by changing your test_python_file.py to the full path to where that file is.

                      1 Reply Last reply
                      2

                      • Login

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