embed a python function into my Qt App.
-
@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.
-
@vinaygopal Can you show your pro file?
-
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
-
@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
-
@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?
-
@vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.
-
@jsulm i am building my apps in the 64 bit configuration. shoudl i change it to 32 bit and try building the program again?
-
@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
-
@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.
-
@vinaygopal Can you show your current pro file?
Also did you a clean rebuild? -
@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
-
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! :)
-
@vinaygopal Looks correct to me. I would try this:
- Delete build directory
- Run qmake
- build
-
@vinaygopal how dead set are you on Python as the scripting language?
If it's not dictated and just a random selection, I'll want to offer 2 alternatives
https://doc.qt.io/qt-5/qscriptengine.html#details
and
https://doc.qt.io/qt-5/qjsengine.html#detailsfor QScript and JavaScript respectively, no external libs required
-
@vinaygopal said in embed a python function into my Qt App.:
@vinaygopal originally wrote just: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.
If that is indeed all he wants to do, he may not need any integrated scripting, he may just need to make his
QProcess
attempt work and we would be done....If he would say why/what the script is supposed to do which requires embedding, we would all know what is required....
-
@JonB @jsulm @J-Hilk ok alright! i give up on embedding python into my program! it just doesn't seem to work however this is the reason for me to use Python, i have a file in my local desktop and i have to send it to the cloud and make the image uploaded a public image these cases can be easily achieved using python if i have to do it with javascript i will have to do it using node JS which will complicate the app, so from QProcess i am able to open the python file inside the python file i have a function called upload blob and it takes two arguments how do i call this function blob and pass those 2 arguments to them using QProcess().
My C++ code
QApplication a(argc, argv); qDebug()<<"working"<<endl; int result=QProcess::execute("C:/Users/vinay/AppData/Local/Programs/Python/Python38/python.exe",QStringList("C:/Users/vinay/Documents/first_qml_training/example_project/upload_to_cloud.py")<<"C:/Users/vinay/Documents/python folder/player1.png"<<"tank.png"); qDebug()<<result<<endl;
My python function
#!/usr/bin/env python # -*- coding: utf-8 -*- from gcloud import storage import os os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:/Users/vinay/Downloads/My First Example Project-507e91a5b594.json" def upload_blob(source_file_name, destination_blob_name): """Uploads a file to the bucket.""" bucket_name="example_1995" storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name) print('File {} uploaded to {}.'.format( source_file_name, destination_blob_name)) print("i am working")
"
-
@vinaygopal
Which is why I asked as I did at the start! Really wish you had said!So you simply want to pass some string filenames to a script which does something, right? No need for integrated scripting.
You need to tell us what actually happens/goes wrong!
First just make your Python script print out everything in
sys.argv
so we can see.Oh, I see, you haven't twigged that at all. You can't just write a function in a standalone Python script/program. You need to have a main program in your script, and access
argv
for the command-line arguments!import sys # Main entry point of app if __name__ == "__main__": # args = sys.argv # count = len(sys.argv) while i < len(sys.argv): arg = sys.argv[i] print(arg)
There are millions of examples on the web.
Separately, you'd better hope all your commands etc are happy with those
/
path separators rather than native\
. I don't want to debate this again with others: some Windows stuff is happy with/
, some is not. -
This post is deleted!
-
@vinaygopal
Why in the world would you try to accesssys.argv[4:5]
if you have said there 3 arguments?Please copy my code and run, then you will see your args are
sys.argv[1:3]
. You can pass those as appropriate to any functions you write.Yes, you are already passing your arguments on the command line from your C++
QProcess::execute()
call.Please do a little printing/debugging yourself to discover this.
-
@JonB sorry! i wasn't able to understand the code flow i made that comment thank you so much. this was such a fun ride.
i was able to achieve what i wanted to do. this forum is really helpful.
Thank you.
Post 20 of 32