Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to call specific Python functions from QT GUI
Forum Updated to NodeBB v4.3 + New Features

How to call specific Python functions from QT GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 3 Posters 2.9k 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.
  • mrjjM mrjj

    Hi
    something like this
    https://sites.northwestern.edu/yihanzhang/2019/08/22/how-to-invoke-python-function-from-c/

    If the use case is simply.

    You can also use something like
    https://pybind11.readthedocs.io/en/stable/advanced/pycpp/index.html

    To create a c++ binding from your python code

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

    @mrjj The problem I get with that now is that qt cannot find 'Python.h'

    In my .pro file I have

    INCLUDEPATH += C:\Python27-32\include\Python.h
    LIBS += -lpython2.7
    

    and then in my main I have

    #include <Python27-32/include/Python.h>
    

    The error message I get is

    Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
    

    I can see 'Python.h' in that folder so I know that is the correct path. Any ideas?

    JonBJ 1 Reply Last reply
    0
    • L lfreeman6490

      @mrjj The problem I get with that now is that qt cannot find 'Python.h'

      In my .pro file I have

      INCLUDEPATH += C:\Python27-32\include\Python.h
      LIBS += -lpython2.7
      

      and then in my main I have

      #include <Python27-32/include/Python.h>
      

      The error message I get is

      Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
      

      I can see 'Python.h' in that folder so I know that is the correct path. Any ideas?

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

      @lfreeman6490
      INCLUDEPATH should be a path to a directory, where it will seek included files. So I would have thought .pro should have:

      INCLUDEPATH += C:\Python27-32\include
      

      and then source file should have one or other of:

      #include <Python.h>
      #include "Python.h"
      

      Does that get you through compilation?

      Then separately I don't know whether

      LIBS += -lpython2.7
      

      will be OK without telling it what directory either python2.7.lib or libpython2.7.a live in. You'll know that at link (rather then compile) time if you get an error/missing symbols.

      L 1 Reply Last reply
      1
      • JonBJ JonB

        @lfreeman6490
        INCLUDEPATH should be a path to a directory, where it will seek included files. So I would have thought .pro should have:

        INCLUDEPATH += C:\Python27-32\include
        

        and then source file should have one or other of:

        #include <Python.h>
        #include "Python.h"
        

        Does that get you through compilation?

        Then separately I don't know whether

        LIBS += -lpython2.7
        

        will be OK without telling it what directory either python2.7.lib or libpython2.7.a live in. You'll know that at link (rather then compile) time if you get an error/missing symbols.

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

        @JonB I tried those, getting the same error

        JonBJ 1 Reply Last reply
        0
        • L lfreeman6490

          @JonB I tried those, getting the same error

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

          @lfreeman6490 said in How to call specific Python functions from QT GUI:

          @JonB I tried those, getting the same error

          I do not believe that statement. The error you pasted was

          Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory

          I do not believe you could get that if you made the changes I stated.

          L 1 Reply Last reply
          1
          • JonBJ JonB

            @lfreeman6490 said in How to call specific Python functions from QT GUI:

            @JonB I tried those, getting the same error

            I do not believe that statement. The error you pasted was

            Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory

            I do not believe you could get that if you made the changes I stated.

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

            @JonB I made those changes. My .pro file now has

            INCLUDEPATH += C:\Python27-32\include
            LIBS += -lpython2.7
            

            and then I tried both

            #include <Python.h>
            #include "Python.h"
            

            along with giving the full path in the include statement, all of them gave me the same error. The error message changes based on what is being included. Instead of it being

            Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
            

            it is

            Cannot open include file: 'Python.h'. No such file or directory
            
            JonBJ 1 Reply Last reply
            0
            • L lfreeman6490

              @JonB I made those changes. My .pro file now has

              INCLUDEPATH += C:\Python27-32\include
              LIBS += -lpython2.7
              

              and then I tried both

              #include <Python.h>
              #include "Python.h"
              

              along with giving the full path in the include statement, all of them gave me the same error. The error message changes based on what is being included. Instead of it being

              Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
              

              it is

              Cannot open include file: 'Python.h'. No such file or directory
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @lfreeman6490
              One more time: how could your code error with

              Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
              

              if you no longer have any reference to

              #include <Python27-32/include/Python.h>
              

              in your code?

              UPDATE
              You have now changed your previous post to a different error message, having stated twice you that you got the error message.

              L 1 Reply Last reply
              1
              • JonBJ JonB

                @lfreeman6490
                One more time: how could your code error with

                Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
                

                if you no longer have any reference to

                #include <Python27-32/include/Python.h>
                

                in your code?

                UPDATE
                You have now changed your previous post to a different error message, having stated twice you that you got the error message.

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

                @JonB As I said, it changed from

                Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
                

                to

                Cannot open include file: 'Python.h'. No such file or directory
                
                JonBJ 1 Reply Last reply
                0
                • L lfreeman6490

                  @JonB As I said, it changed from

                  Cannot open include file: 'Python27-32/include/Python.h'. No such file or directory
                  

                  to

                  Cannot open include file: 'Python.h'. No such file or directory
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #10

                  @lfreeman6490
                  No, you did not state that. You stated that the error message was the same. When it was not. Which I then have to deal with....

                  Please state error messages correctly.

                  Let's start with: what OS are you on?

                  L 2 Replies Last reply
                  1
                  • JonBJ JonB

                    @lfreeman6490
                    No, you did not state that. You stated that the error message was the same. When it was not. Which I then have to deal with....

                    Please state error messages correctly.

                    Let's start with: what OS are you on?

                    L Offline
                    L Offline
                    lfreeman6490
                    wrote on last edited by
                    #11
                    This post is deleted!
                    JonBJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @lfreeman6490
                      No, you did not state that. You stated that the error message was the same. When it was not. Which I then have to deal with....

                      Please state error messages correctly.

                      Let's start with: what OS are you on?

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

                      @JonB I'm on windows 10
                      Python 2.7, 32 bit
                      QTCreator 3.0.1

                      1 Reply Last reply
                      0
                      • L lfreeman6490

                        This post is deleted!

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

                        @lfreeman6490 said in How to call specific Python functions from QT GUI:

                        Either way, it is the same error

                        No, it is not. It has different text, which I need if I am able to help you. Up to you whether you want help, but I'm not going to spend time when someone says an error is the same when it is different. You might know what's on your screen, I do not.

                        After you made the change to the .pro file, did you re-run qmake?

                        L 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @lfreeman6490 said in How to call specific Python functions from QT GUI:

                          Either way, it is the same error

                          No, it is not. It has different text, which I need if I am able to help you. Up to you whether you want help, but I'm not going to spend time when someone says an error is the same when it is different. You might know what's on your screen, I do not.

                          After you made the change to the .pro file, did you re-run qmake?

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

                          @JonB Yes I did run QMake, it's now telling me

                          LNK1104: cannot open file 'python27_d.lib'
                          
                          JonBJ 1 Reply Last reply
                          0
                          • L lfreeman6490

                            @JonB Yes I did run QMake, it's now telling me

                            LNK1104: cannot open file 'python27_d.lib'
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #15

                            @lfreeman6490
                            So when you ran qmake the include error went away?

                            You are now in the link situation I described. You'd better tell the linker, via the .pro file, where to look for the python27_d.lib it is seeking from your LIBS += -lpython2.7 .... (Though I don't think from the error mesaage it can come from your LIBS += -lpython2.7.)

                            L 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @lfreeman6490
                              So when you ran qmake the include error went away?

                              You are now in the link situation I described. You'd better tell the linker, via the .pro file, where to look for the python27_d.lib it is seeking from your LIBS += -lpython2.7 .... (Though I don't think from the error mesaage it can come from your LIBS += -lpython2.7.)

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

                              @JonB I do not have a 'python27_d.lib' file in my python download at all, I only have 'python27.lib'. I'm looking where to get one from

                              JonBJ 1 Reply Last reply
                              0
                              • L lfreeman6490

                                @JonB I do not have a 'python27_d.lib' file in my python download at all, I only have 'python27.lib'. I'm looking where to get one from

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

                                @lfreeman6490
                                The _d suffix looks like it's looking for a debug version of the library, that's all I know.

                                1 Reply Last reply
                                1
                                • L Offline
                                  L Offline
                                  lfreeman6490
                                  wrote on last edited by
                                  #18

                                  For anybody in the future that may come across this. Thanks to @JonB I was able to have in my .pro file

                                  INCLUDEPATH += C:/Python27/include
                                  
                                  win32:LIBS += -LC:/Python27/libs -lpython27
                                  

                                  and then in my main.cpp file

                                  #ifdef _DEBUG
                                      #undef _DEBUG
                                      #include <Python.h>
                                  #else
                                      #include <Python.h>
                                  #endif
                                  

                                  The reason for the if loop is to circumvent the debug python.h file being loaded. By providing both routes the path to 'python.h'

                                  Found from
                                  https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work

                                  JonBJ 1 Reply Last reply
                                  3
                                  • L lfreeman6490

                                    For anybody in the future that may come across this. Thanks to @JonB I was able to have in my .pro file

                                    INCLUDEPATH += C:/Python27/include
                                    
                                    win32:LIBS += -LC:/Python27/libs -lpython27
                                    

                                    and then in my main.cpp file

                                    #ifdef _DEBUG
                                        #undef _DEBUG
                                        #include <Python.h>
                                    #else
                                        #include <Python.h>
                                    #endif
                                    

                                    The reason for the if loop is to circumvent the debug python.h file being loaded. By providing both routes the path to 'python.h'

                                    Found from
                                    https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work

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

                                    @lfreeman6490 said in How to call specific Python functions from QT GUI:

                                    Found from
                                    https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work

                                    Good spot! That's a nasty one!

                                    For anyone following this link to the stackoverflow topic, you should look at https://stackoverflow.com/a/32425901/489865 answer on that page, not just the accepted solution one.

                                    L 1 Reply Last reply
                                    2
                                    • JonBJ JonB

                                      @lfreeman6490 said in How to call specific Python functions from QT GUI:

                                      Found from
                                      https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work

                                      Good spot! That's a nasty one!

                                      For anyone following this link to the stackoverflow topic, you should look at https://stackoverflow.com/a/32425901/489865 answer on that page, not just the accepted solution one.

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

                                      @JonB Would you know how should I proceed with my original issue, that is calling specific Python functions. The path I've been trying to down isn't getting me very far. I've been using this so far.

                                      void f_pathloss::on_pb_connect_disconnect_clicked()
                                      {
                                          PyObject *pName, *pModule, *pDict, *pFunc;
                                          PyObject *pArgs, *pValue;
                                      
                                          Py_Initialize();
                                      
                                          pName = PyString_FromString((char*)"test_program");
                                          pModule = PyImport_Import(pName);
                                      
                                          pFunc = PyObject_GetAttrString(pModule, "test_function");
                                      
                                          PyXDECREF(pFunc);
                                          pValue = PyObject_CallObject(pFunc, pArgs);
                                      }
                                      

                                      where 'test_function' is in 'test_program'.py. All that test_function does is return a string that simply says "this is a c++ test". I have seen QProcess mentioned, but that seems to mainly work when you want to run the entire script, not just a single function.

                                      For the code I've pasted above, once the user hits the connect_disconnect_clicked() button, I want to call a function.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • L lfreeman6490

                                        @JonB Would you know how should I proceed with my original issue, that is calling specific Python functions. The path I've been trying to down isn't getting me very far. I've been using this so far.

                                        void f_pathloss::on_pb_connect_disconnect_clicked()
                                        {
                                            PyObject *pName, *pModule, *pDict, *pFunc;
                                            PyObject *pArgs, *pValue;
                                        
                                            Py_Initialize();
                                        
                                            pName = PyString_FromString((char*)"test_program");
                                            pModule = PyImport_Import(pName);
                                        
                                            pFunc = PyObject_GetAttrString(pModule, "test_function");
                                        
                                            PyXDECREF(pFunc);
                                            pValue = PyObject_CallObject(pFunc, pArgs);
                                        }
                                        

                                        where 'test_function' is in 'test_program'.py. All that test_function does is return a string that simply says "this is a c++ test". I have seen QProcess mentioned, but that seems to mainly work when you want to run the entire script, not just a single function.

                                        For the code I've pasted above, once the user hits the connect_disconnect_clicked() button, I want to call a function.

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

                                        @lfreeman6490
                                        I have never done any of this. You show code, which looks reasonable, but say nothing about what happens when you call it, or step through it in debugger? @mrjj linked to an example, it looks similar. You would benefit from some error checking code here.

                                        QProcess is to do with running an external program, or OS command. You can run a whole Python script that way, but I don't think it's what you should be looking at.

                                        L 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @lfreeman6490
                                          I have never done any of this. You show code, which looks reasonable, but say nothing about what happens when you call it, or step through it in debugger? @mrjj linked to an example, it looks similar. You would benefit from some error checking code here.

                                          QProcess is to do with running an external program, or OS command. You can run a whole Python script that way, but I don't think it's what you should be looking at.

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

                                          @JonB Well nothing happens when I do it. It builds successfully and launches the GUI, once I click the button nothing happens. There isn't an error thrown at all either. Just wanted to ask and see

                                          JonBJ 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