How to call specific Python functions from QT GUI
-
@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.
-
@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.
@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. -
@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.@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
-
@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
@lfreeman6490
So start debugging that function! I take it you actually have atest_function
in thetest_program
, else what do you expect.Maybe you need the different code in https://sites.northwestern.edu/yihanzhang/2019/08/22/how-to-invoke-python-function-from-c/, I don't know?