I want to use the xlwt library in an application made in Qt. As it's the first time I'm trying it, I was doing some tests to see how the call is made, according to this page:
https://docs.python.org/2/extending/embedding.html
I create a python script, calledprueba.py
print "Hello world!"
Now I want to call it from a C++ application:
#include <Python.h>
#include <QDebug>
int main(int argc, char *argv[])
{
Py_Initialize();
PyObject* ruta = PyString_FromString("/home/usuario/programacion/prueba.py");
PyObject* modulo = PyImport_Import(ruta);
if( modulo)
{
qDebug()<<"Hay funcion";
}
Py_Finalize();
return 0;
}
But you see that '''modulo''' follows 0, nothing happens inside the braces. I don't know what error I'll have, but I can't even start testing a function with a more serious script, because I can't even load the module.
Edit:
This way if it works (I put it as a solution):
#include <Python.h>
#include <QDebug>
int main(int argc, char *argv[])
{
const char *scriptDirectoryName = "/home/usuario/programacion/";
Py_Initialize();
PyObject *sysPath = PySys_GetObject("path");
PyObject *path = PyString_FromString(scriptDirectoryName);
int result = PyList_Insert(sysPath, 0, path);
PyObject *pModule = PyImport_ImportModule("prueba.py");
if (PyErr_Occurred())
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
finally a way to call a python script can be this: