How to call specific Python functions from QT GUI
-
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.
-
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.htmlTo create a c++ binding from your python code
-
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.htmlTo create a c++ binding from your python code
@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?
-
@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?
@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
orlibpython2.7.a
live in. You'll know that at link (rather then compile) time if you get an error/missing symbols. -
@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
orlibpython2.7.a
live in. You'll know that at link (rather then compile) time if you get an error/missing symbols.@JonB I tried those, getting the same error
-
@JonB I tried those, getting the same error
@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.
-
@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.
@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
-
@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
@lfreeman6490
One more time: how could your code error withCannot 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. -
@lfreeman6490
One more time: how could your code error withCannot 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.@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
-
@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
@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?
-
@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?
This post is deleted! -
@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?
@JonB I'm on windows 10
Python 2.7, 32 bit
QTCreator 3.0.1 -
This post is deleted!
@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-runqmake
? -
@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-runqmake
?@JonB Yes I did run QMake, it's now telling me
LNK1104: cannot open file 'python27_d.lib'
-
@JonB Yes I did run QMake, it's now telling me
LNK1104: cannot open file 'python27_d.lib'
@lfreeman6490
So when you ranqmake
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 thepython27_d.lib
it is seeking from yourLIBS += -lpython2.7
.... (Though I don't think from the error mesaage it can come from yourLIBS += -lpython2.7
.) -
@lfreeman6490
So when you ranqmake
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 thepython27_d.lib
it is seeking from yourLIBS += -lpython2.7
.... (Though I don't think from the error mesaage it can come from yourLIBS += -lpython2.7
.)@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
-
@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
@lfreeman6490
The_d
suffix looks like it's looking for a debug version of the library, that's all I know. -
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 -
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@lfreeman6490 said in How to call specific Python functions from QT GUI:
Found from
https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-workGood 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.
-
@lfreeman6490 said in How to call specific Python functions from QT GUI:
Found from
https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-workGood 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.
@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.