Cannot open file - QProcess
-
Hello, I am trying to start a python script with QProcess on my Raspberry. I have tried the docu, but the following code doesn't start my python file:
The python-file does only create a .txt-file and works if I run it.communication.cpp:
#include "communication.h" #include <QProcess> communication::communication() { } communication::~communication() { } void communication::conPy() { QProcess *process = new QProcess(this); QString program = "test.py"; QString folder = "/home/myFolder/test.py"; process->start(program, QStringList() << folder);```
communication.h:
#ifndef COMMUNICATION_H #define COMMUNICATION_H #include <QObject> class communication : public QObject { Q_OBJECT public: communication(); ~communication(); void conPy(); }; #endif // COMMUNICATION_H
main.cpp:
#include <QCoreApplication> #include <communication.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); communication comm; comm.comPy(); return a.exe(); }
fp = open('test.txt', 'x') fp.close()
-
Hello, I am trying to start a python script with QProcess on my Raspberry. I have tried the docu, but the following code doesn't start my python file:
The python-file does only create a .txt-file and works if I run it.communication.cpp:
#include "communication.h" #include <QProcess> communication::communication() { } communication::~communication() { } void communication::conPy() { QProcess *process = new QProcess(this); QString program = "test.py"; QString folder = "/home/myFolder/test.py"; process->start(program, QStringList() << folder);```
communication.h:
#ifndef COMMUNICATION_H #define COMMUNICATION_H #include <QObject> class communication : public QObject { Q_OBJECT public: communication(); ~communication(); void conPy(); }; #endif // COMMUNICATION_H
main.cpp:
#include <QCoreApplication> #include <communication.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); communication comm; comm.comPy(); return a.exe(); }
fp = open('test.txt', 'x') fp.close()
@Chaki said in Cannot open file - QProcess:
QString program = "test.py";
QString folder = "/home/myFolder/test.py";
process->start(program, QStringList() << folder);Should rather be:
QString program = "/home/myFolder/test.py"; process->start(program, QStringList());
Don't forget to delete the QProcess instance when not needed anymore, else you will have a memory leak.
-
Thank you for your answer, I know that it works with following code:
void communication::conPy() { QProcess *process = new QProcess(this); QString program = "/home/someFolder/test.py"; QStringList arguments; arguments << QDir::currentPath()<< "/home/someFolder/test.py"; process->start("python", arguments); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; } }
-
Thank you for your answer, I know that it works with following code:
void communication::conPy() { QProcess *process = new QProcess(this); QString program = "/home/someFolder/test.py"; QStringList arguments; arguments << QDir::currentPath()<< "/home/someFolder/test.py"; process->start("python", arguments); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; } }
-
Thank you for your answer, I know that it works with following code:
void communication::conPy() { QProcess *process = new QProcess(this); QString program = "/home/someFolder/test.py"; QStringList arguments; arguments << QDir::currentPath()<< "/home/someFolder/test.py"; process->start("python", arguments); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; } }
@Chaki said in Cannot open file - QProcess:
arguments << QDir::currentPath()<< "/home/someFolder/test.py";
Why do you pass the path to the executable as parameter?!
-
Thank you for your answer, I know that it works with following code:
void communication::conPy() { QProcess *process = new QProcess(this); QString program = "/home/someFolder/test.py"; QStringList arguments; arguments << QDir::currentPath()<< "/home/someFolder/test.py"; process->start("python", arguments); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; } }
@Chaki
Please first answer @jsulm's questions. If you have not set up your script file to be a properly executable Python file as he states that would be the reason for your problem.However, additionally:
I know that it works with following code:
process->start("python", arguments);
I do not know about RPi, but some Linux distributions require you to use
python3
as the executable.python
may either run Python 2.x or does not have any such executable installed.arguments << QDir::currentPath()<< "/home/someFolder/test.py";
You say that "it works with following code", but that should give you an error like:
/usr/bin/python3: can't find '__main__' module in '/the/QDir/currentPath/directory'
You are sure what you say works without this error, you did actually test it?
-
@jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:
QProcess *process = new QProcess(this); process->start("python3 /home/someFolder/test.py"); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; }
-
@jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:
QProcess *process = new QProcess(this); process->start("python3 /home/someFolder/test.py"); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; }
@Chaki
That is not what @jsulm is asking you. You have already said that you can run your script, even fromQProcess
, withpython3 ...
. What you have asked is why you cannot execute it with justQString program = "/home/someFolder/test.py";
.If you want to know the answer to that, go into a terminal and type
/home/someFolder/test.py
and see what happens.
-
@Chaki
That is not what @jsulm is asking you. You have already said that you can run your script, even fromQProcess
, withpython3 ...
. What you have asked is why you cannot execute it with justQString program = "/home/someFolder/test.py";
.If you want to know the answer to that, go into a terminal and type
/home/someFolder/test.py
and see what happens.
-
@jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:
QProcess *process = new QProcess(this); process->start("python3 /home/someFolder/test.py"); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); } else { qDebug() << "Analyzing done. "; }
@Chaki said in Cannot open file - QProcess:
process->start("python3 /home/someFolder/test.py");
This should be
process->start("python3", QStringList() << "/home/someFolder/test.py");