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. embed a python function into my Qt App.
Forum Updated to NodeBB v4.3 + New Features

embed a python function into my Qt App.

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 4 Posters 6.9k 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.
  • V vinaygopal

    When i click a button, i want to open up the python script (.py file) and run the python script written by me and exit python. I should be able to pass arguments to the python file through Qt c++? please help me out on how to achieve this. i did do some reading about QProcess class but i am not able to achieve this.

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

    @vinaygopal
    You can indeed go down @jsulm's path, but embedding is a lot more involved than just running a subprocess. Which one you need depends on what this script is trying to do/what you want to achieve.

    If you want to pass arguments in/receive arguments back then in-process Python embedding may indeed be best. But as phrased if all you want to do is run an external script with some command-line arguments just like any other OS command but it happens to be written in Python, it really is simple to do with QProcess, should take one minute to write.

    i did do some reading about QProcess class but i am not able to achieve this.

    If you wish to pursue this route, what does this mean? Show some code, it's dead easy. For example, the simplest might be a one-liner using https://doc.qt.io/qt-5/qprocess.html#execute:

    int result = QProcess::execute("python", QStringList() << arg1 << arg2);
    
    
    1 Reply Last reply
    5
    • jsulmJ jsulm

      @vinaygopal Take a look at https://docs.python.org/2/extending/embedding.html No need for QProcess I think.

      V Offline
      V Offline
      vinaygopal
      wrote on last edited by vinaygopal
      #4

      @jsulm i have to include the python library files in order to include Python.h can you please tell me how to specify lib path in QT in order for me to achieve this?

      i am following this tutorial (https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code) now i can include the .h file but linking it with the library i am having some problems as i do not know how to specify the library path as per the tutorial or if you know anyother way this can be achieved please let me know.

      jsulmJ 1 Reply Last reply
      0
      • V vinaygopal

        @jsulm i have to include the python library files in order to include Python.h can you please tell me how to specify lib path in QT in order for me to achieve this?

        i am following this tutorial (https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code) now i can include the .h file but linking it with the library i am having some problems as i do not know how to specify the library path as per the tutorial or if you know anyother way this can be achieved please let me know.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @vinaygopal Please take a look at https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply
        2
        • jsulmJ jsulm

          @vinaygopal Please take a look at https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

          V Offline
          V Offline
          vinaygopal
          wrote on last edited by
          #6

          @jsulm i have done the changes

          INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
          
          wind32:LIBS += C:/Users/vinay/AppData/Local/Programs/Python/Python37-32/libs
          
          
          
          

          i have done the changes as per the document my include file works that is python.h is found but it is not able to link to the library as the path specified in the LIBS is a the directory to a bunch of libraries in python. i get this warning at the top of the screen

          "the code model could not parse an input file,which might lead to incorrect code completion"

          jsulmJ 1 Reply Last reply
          0
          • V vinaygopal

            @jsulm i have done the changes

            INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
            
            wind32:LIBS += C:/Users/vinay/AppData/Local/Programs/Python/Python37-32/libs
            
            
            
            

            i have done the changes as per the document my include file works that is python.h is found but it is not able to link to the library as the path specified in the LIBS is a the directory to a bunch of libraries in python. i get this warning at the top of the screen

            "the code model could not parse an input file,which might lead to incorrect code completion"

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @vinaygopal said in embed a python function into my Qt App.:

            "the code model could not parse an input file,which might lead to incorrect code completion"

            You can ignore this.

            Can you show the exact linker error?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            V 2 Replies Last reply
            1
            • jsulmJ jsulm

              @vinaygopal said in embed a python function into my Qt App.:

              "the code model could not parse an input file,which might lead to incorrect code completion"

              You can ignore this.

              Can you show the exact linker error?

              V Offline
              V Offline
              vinaygopal
              wrote on last edited by
              #8
              This post is deleted!
              1 Reply Last reply
              0
              • jsulmJ jsulm

                @vinaygopal said in embed a python function into my Qt App.:

                "the code model could not parse an input file,which might lead to incorrect code completion"

                You can ignore this.

                Can you show the exact linker error?

                V Offline
                V Offline
                vinaygopal
                wrote on last edited by
                #9

                @jsulm these are the errors i get

                this is my main fail

                #include <QApplication>
                #include <QDebug>
                #include <Python.h>
                
                
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    qDebug()<<"working"<<endl;
                
                
                    return a.exec();
                

                these are the errors

                C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\pytime.h:6: In file included from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/pytime.h:6:0,
                
                C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\Python.h:87: from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/Python.h:87,
                
                C:\Users\vinay\Documents\first_qml_training\example_project\main.cpp:6: from ..\example_project\main.cpp:6:
                
                C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\object.h:448: error: expected unqualified-id before ';' token
                     PyType_Slot *slots; /* terminated by slot==0. */
                
                                   ^
                
                jsulmJ 1 Reply Last reply
                0
                • V vinaygopal

                  @jsulm these are the errors i get

                  this is my main fail

                  #include <QApplication>
                  #include <QDebug>
                  #include <Python.h>
                  
                  
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      qDebug()<<"working"<<endl;
                  
                  
                      return a.exec();
                  

                  these are the errors

                  C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\pytime.h:6: In file included from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/pytime.h:6:0,
                  
                  C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\Python.h:87: from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/Python.h:87,
                  
                  C:\Users\vinay\Documents\first_qml_training\example_project\main.cpp:6: from ..\example_project\main.cpp:6:
                  
                  C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\object.h:448: error: expected unqualified-id before ';' token
                       PyType_Slot *slots; /* terminated by slot==0. */
                  
                                     ^
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @vinaygopal This is not linker error but compiler.
                  Try to include the header like this:

                  extern "C"
                  {
                    #include <Python.h>
                  }
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @vinaygopal This is not linker error but compiler.
                    Try to include the header like this:

                    extern "C"
                    {
                      #include <Python.h>
                    }
                    
                    V Offline
                    V Offline
                    vinaygopal
                    wrote on last edited by
                    #11

                    @jsulm I tried it and i got the same set of errors however when i tried with the right click and add library method i get this following error

                    :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a', needed by 'debug/example_project.exe'.  Stop.
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • V vinaygopal

                      @jsulm I tried it and i got the same set of errors however when i tried with the right click and add library method i get this following error

                      :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a', needed by 'debug/example_project.exe'.  Stop.
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @vinaygopal Can you show your pro file?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      V 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @vinaygopal Can you show your pro file?

                        V Offline
                        V Offline
                        vinaygopal
                        wrote on last edited by
                        #13

                        @jsulm

                        QT       += core gui networkauth
                        
                        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                        
                        TARGET = example_project
                        TEMPLATE = app
                        
                        
                        # The following define makes your compiler emit warnings if you use
                        # any feature of Qt which has been marked as deprecated (the exact warnings
                        # depend on your compiler). Please consult the documentation of the
                        # deprecated API in order to know how to port your code away from it.
                        DEFINES += QT_DEPRECATED_WARNINGS
                        
                        # You can also make your code fail to compile if you use deprecated APIs.
                        # In order to do so, uncomment the following line.
                        # You can also select to disable deprecated APIs only up to a certain version of Qt.
                        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                        
                        CONFIG += c++11
                        
                        SOURCES += \
                                main.cpp \
                                google_widget.cpp
                        
                        HEADERS += \
                                google_widget.h
                        
                        FORMS += \
                                google_widget.ui
                        
                        # Default rules for deployment.
                        qnx: target.path = /tmp/$${TARGET}/bin
                        else: unix:!android: target.path = /opt/$${TARGET}/bin
                        !isEmpty(target.path): INSTALLS += target
                        
                        DISTFILES += \
                            upload_to_cloud.py
                        
                        
                        #CONFIG += no_lflags_merge
                        
                        INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
                        
                        
                        
                        win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                        else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37d
                        else:unix: LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                        
                        
                        win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                        else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a
                        else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                        else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37d.lib
                        else:unix: PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                        
                        
                        jsulmJ 1 Reply Last reply
                        0
                        • V vinaygopal

                          @jsulm

                          QT       += core gui networkauth
                          
                          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                          
                          TARGET = example_project
                          TEMPLATE = app
                          
                          
                          # The following define makes your compiler emit warnings if you use
                          # any feature of Qt which has been marked as deprecated (the exact warnings
                          # depend on your compiler). Please consult the documentation of the
                          # deprecated API in order to know how to port your code away from it.
                          DEFINES += QT_DEPRECATED_WARNINGS
                          
                          # You can also make your code fail to compile if you use deprecated APIs.
                          # In order to do so, uncomment the following line.
                          # You can also select to disable deprecated APIs only up to a certain version of Qt.
                          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                          
                          CONFIG += c++11
                          
                          SOURCES += \
                                  main.cpp \
                                  google_widget.cpp
                          
                          HEADERS += \
                                  google_widget.h
                          
                          FORMS += \
                                  google_widget.ui
                          
                          # Default rules for deployment.
                          qnx: target.path = /tmp/$${TARGET}/bin
                          else: unix:!android: target.path = /opt/$${TARGET}/bin
                          !isEmpty(target.path): INSTALLS += target
                          
                          DISTFILES += \
                              upload_to_cloud.py
                          
                          
                          #CONFIG += no_lflags_merge
                          
                          INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
                          
                          
                          
                          win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                          else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37d
                          else:unix: LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                          
                          
                          win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                          else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a
                          else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                          else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37d.lib
                          else:unix: PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                          
                          
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @vinaygopal On Windows it should be:

                          LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                          

                          See https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          V 1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @vinaygopal On Windows it should be:

                            LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                            

                            See https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

                            V Offline
                            V Offline
                            vinaygopal
                            wrote on last edited by
                            #15

                            @jsulm i did add the library as you had recomended it still did'nt work and more importantly i figured the module python.h and its equivalent library is not compatible with mingw gcc compiler it is only compatible with microsoft specific compilers as suggested by this question (https://stackoverflow.com/questions/6731100/link-to-python-with-mingw) they have provided an answer but is there a simpler way to get this done?

                            jsulmJ 1 Reply Last reply
                            0
                            • V vinaygopal

                              @jsulm i did add the library as you had recomended it still did'nt work and more importantly i figured the module python.h and its equivalent library is not compatible with mingw gcc compiler it is only compatible with microsoft specific compilers as suggested by this question (https://stackoverflow.com/questions/6731100/link-to-python-with-mingw) they have provided an answer but is there a simpler way to get this done?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              V 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.

                                V Offline
                                V Offline
                                vinaygopal
                                wrote on last edited by
                                #17

                                @jsulm i am building my apps in the 64 bit configuration. shoudl i change it to 32 bit and try building the program again?

                                jsulmJ 1 Reply Last reply
                                0
                                • V vinaygopal

                                  @jsulm i am building my apps in the 64 bit configuration. shoudl i change it to 32 bit and try building the program again?

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #18

                                  @vinaygopal said in embed a python function into my Qt App.:

                                  shoudl i change it to 32 bit

                                  Yes, you can't mix 32bit and 64bit

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  V 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @vinaygopal said in embed a python function into my Qt App.:

                                    shoudl i change it to 32 bit

                                    Yes, you can't mix 32bit and 64bit

                                    V Offline
                                    V Offline
                                    vinaygopal
                                    wrote on last edited by
                                    #19

                                    @jsulm uninstalled python 32 bit and reinstalled 64bit version of python and included the libraries still the same error

                                    :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python38/libs/libpython38d.a', needed by 'debug/example_project.exe'. Stop.

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • V vinaygopal

                                      @jsulm uninstalled python 32 bit and reinstalled 64bit version of python and included the libraries still the same error

                                      :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python38/libs/libpython38d.a', needed by 'debug/example_project.exe'. Stop.

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @vinaygopal Can you show your current pro file?
                                      Also did you a clean rebuild?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      V 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @vinaygopal Can you show your current pro file?
                                        Also did you a clean rebuild?

                                        V Offline
                                        V Offline
                                        vinaygopal
                                        wrote on last edited by
                                        #21

                                        @jsulm yes i clean the build run qmake and then build the file everytime i change the pro file

                                        #-------------------------------------------------
                                        #
                                        # Project created by QtCreator 2019-11-21T10:31:03
                                        #
                                        #-------------------------------------------------
                                        
                                        QT       += core gui networkauth
                                        
                                        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                        
                                        TARGET = example_project
                                        TEMPLATE = app
                                        
                                        
                                        # The following define makes your compiler emit warnings if you use
                                        # any feature of Qt which has been marked as deprecated (the exact warnings
                                        # depend on your compiler). Please consult the documentation of the
                                        # deprecated API in order to know how to port your code away from it.
                                        DEFINES += QT_DEPRECATED_WARNINGS
                                        
                                        # You can also make your code fail to compile if you use deprecated APIs.
                                        # In order to do so, uncomment the following line.
                                        # You can also select to disable deprecated APIs only up to a certain version of Qt.
                                        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                                        
                                        CONFIG += c++11
                                        
                                        SOURCES += \
                                                main.cpp \
                                                google_widget.cpp
                                        
                                        HEADERS += \
                                                google_widget.h
                                        
                                        FORMS += \
                                                google_widget.ui
                                        
                                        # Default rules for deployment.
                                        qnx: target.path = /tmp/$${TARGET}/bin
                                        else: unix:!android: target.path = /opt/$${TARGET}/bin
                                        !isEmpty(target.path): INSTALLS += target
                                        
                                        DISTFILES += \
                                            upload_to_cloud.py
                                        
                                        
                                        #CONFIG += no_lflags_merge
                                        
                                        INCLUDEPATH="C:/Users/vinay/AppData/Local/Programs/Python/Python38/include"
                                        LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python38/libs/python38.lib
                                        
                                        jsulmJ 1 Reply Last reply
                                        0
                                        • JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #22

                                          I'm keeping an eye on this thread. As I said right at the start

                                          You can indeed go down @jsulm's path, but embedding is a lot more involved than just running a subprocess.
                                          But as phrased if all you want to do is run an external script with some command-line arguments [...], it really is simple to do with QProcess, should take one minute to write.

                                          I do hope/presume that (for both your sakes) for use case here all this embedding is indeed required and worth the effort! :)

                                          V 1 Reply Last reply
                                          1

                                          • Login

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