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.8k 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 Offline
    L Offline
    lfreeman6490
    wrote on 24 Aug 2021, 13:27 last edited by lfreeman6490
    #1

    I have a .py program and a c++ GUI, I am trying to link buttons in the c++ gui to functions in the python program. I have seen how to execute entire scripts using QProcess, but I am not trying to execute the whole script, I want to call a specific function, execute that function, and then return the results.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 24 Aug 2021, 14:12 last edited by
      #2

      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 1 Reply Last reply 24 Aug 2021, 14:51
      1
      • M mrjj
        24 Aug 2021, 14:12

        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 24 Aug 2021, 14:51 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?

        J 1 Reply Last reply 24 Aug 2021, 15:13
        0
        • L lfreeman6490
          24 Aug 2021, 14:51

          @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?

          J Offline
          J Offline
          JonB
          wrote on 24 Aug 2021, 15:13 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 24 Aug 2021, 15:15
          1
          • J JonB
            24 Aug 2021, 15:13

            @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 24 Aug 2021, 15:15 last edited by
            #5

            @JonB I tried those, getting the same error

            J 1 Reply Last reply 24 Aug 2021, 15:17
            0
            • L lfreeman6490
              24 Aug 2021, 15:15

              @JonB I tried those, getting the same error

              J Offline
              J Offline
              JonB
              wrote on 24 Aug 2021, 15:17 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 24 Aug 2021, 15:19
              1
              • J JonB
                24 Aug 2021, 15:17

                @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 24 Aug 2021, 15:19 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
                
                J 1 Reply Last reply 24 Aug 2021, 15:20
                0
                • L lfreeman6490
                  24 Aug 2021, 15:19

                  @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
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 24 Aug 2021, 15:20 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 24 Aug 2021, 15:22
                  1
                  • J JonB
                    24 Aug 2021, 15:20

                    @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 24 Aug 2021, 15:22 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
                    
                    J 1 Reply Last reply 24 Aug 2021, 15:22
                    0
                    • L lfreeman6490
                      24 Aug 2021, 15:22

                      @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
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 24 Aug 2021, 15:22 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 24 Aug 2021, 15:23
                      1
                      • J JonB
                        24 Aug 2021, 15:22

                        @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 24 Aug 2021, 15:23 last edited by
                        #11
                        This post is deleted!
                        J 1 Reply Last reply 24 Aug 2021, 15:25
                        0
                        • J JonB
                          24 Aug 2021, 15:22

                          @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 24 Aug 2021, 15:24 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
                            24 Aug 2021, 15:23

                            This post is deleted!

                            J Offline
                            J Offline
                            JonB
                            wrote on 24 Aug 2021, 15:25 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 24 Aug 2021, 15:51
                            1
                            • J JonB
                              24 Aug 2021, 15:25

                              @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 24 Aug 2021, 15:51 last edited by
                              #14

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

                              LNK1104: cannot open file 'python27_d.lib'
                              
                              J 1 Reply Last reply 24 Aug 2021, 15:55
                              0
                              • L lfreeman6490
                                24 Aug 2021, 15:51

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

                                LNK1104: cannot open file 'python27_d.lib'
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 24 Aug 2021, 15:55 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 24 Aug 2021, 15:57
                                1
                                • J JonB
                                  24 Aug 2021, 15:55

                                  @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 24 Aug 2021, 15:57 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

                                  J 1 Reply Last reply 24 Aug 2021, 15:58
                                  0
                                  • L lfreeman6490
                                    24 Aug 2021, 15:57

                                    @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

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 24 Aug 2021, 15:58 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 24 Aug 2021, 16:02 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

                                      J 1 Reply Last reply 24 Aug 2021, 16:13
                                      3
                                      • L lfreeman6490
                                        24 Aug 2021, 16:02

                                        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

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 24 Aug 2021, 16:13 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 24 Aug 2021, 17:13
                                        2
                                        • J JonB
                                          24 Aug 2021, 16:13

                                          @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 24 Aug 2021, 17:13 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.

                                          J 1 Reply Last reply 24 Aug 2021, 17:26
                                          0

                                          7/23

                                          24 Aug 2021, 15:19

                                          16 unread
                                          • Login

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