Reputation: 109
I have a Python script that takes three folders as input. I am trying to create a GUI in which I browse for the three folder locations, then call the Python script with those as parameters. I've already create the GUI and can browse for the folder locations, but I seem to have problems calling the Python script using either QProcess or Python.h.
QProcess:
QString arg1 = ui->folder1->text();
QString arg2 = ui->folder2->text();
QString arg3 = ui->folder3->text();
QProcess p;
QString script = "python script.py";
QStringList params;
params << arg1 << arg2 << arg3;
p.start(script, params);
p.waitForFinished(-1);
QString p_stdout = p.readAll();
ui->displayOutput->setText(p_stdout);
Python.h:
QString arg1 = ui->folder1->text();
QString arg2 = ui->folder2->text();
QString arg3 = ui->folder3->text();
const char* args1 = arg1.toUtf8().constData();
const char* args2 = arg2.toUtf8().constData();
const char* args3 = arg3.toUtf8().constData();
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pResult, *pArgs;
// Initialize Python Interpreter
Py_Initialize();
// Convert python script's name into Python string
pName = PyString_FromString("script");
// Import the file as a Python module
pModule = PyImport_Import(pName);
// Create a dictionary for the contents of the module
pDict = PyModule_GetDict(pModule);
// Arguments
pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, PyString_FromString(args1));
PyTuple_SetItem(pArgs, 1, PyString_FromString(args2));
PyTuple_SetItem(pArgs, 2, PyString_FromString(args3));
pFunc = PyDict_GetItemString(pDict, "main");
pResult = PyObject_CallObject(pFunc, pArgs);
Py_Finalize();
I will say that when I do run the Python.h in either debug or Release, the program crashes when I press the button to run the code. Is there a better way to run either code without running into issues?
Edit: It is able to run without arguments using the QProcess method, yet will not run when I include the arguments, which are folder paths. I made a function to put quotations to those folders that have whitespace in them, but it still won't run. Is there a way to pass in the folders as parameters so that the Python script can run?
Upvotes: 1
Views: 3105
Reputation: 51
QString script = "python "+ "../folder1/script.py;
const char* cmd = script.toLocal8Bit().constData();
system(cmd);
is not reading the content correctly.
The output shows Invalid Test_log directory always.
But when running the python script.py from command line directly gives proper o/p. Thepython path is set in my envionment variable .
log_dir = '../Test_Logs/'
if not os.path.exists(log_dir):
print('Invalid Test_log directory')
exit()
Upvotes: 0
Reputation: 109
So after trying different methods, this is what works for me on Windows
QString cmd_qt = QString("python %1script.py %2 %3 %4").arg(filename1).arg(filename3).arg(filename4).arg(filename5);
const char* cmd = cmd_qt.toLocal8Bit().constData();
system(cmd);
This just calls the Python function via command prompt. I don't know if this is the most efficient way. but it works for now. If there is a better way, let me know.
Upvotes: 1