Run python script in Qt
-
wrote on 3 Jul 2019, 05:38 last edited by NotYourFan 7 Mar 2019, 05:38
Hey,
im trying to run a python Script in Qt after clicking a button, with the following Code:
void TestWindow::loadFile() QProcess p; QStringList params; params << "createJSON.py"; p.start("python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
i get the following error:
- Process started!
- Error occurred "Process crashed"
- OUT ""
- ERR "Fatal Python error: initfsencoding: unable to load the file system codec\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00005b50 (most recent call first):\n"
If i run the script in my python IDE it works fine.
My "createJSON.py" create a JSON-File so i can checked if the Qt run my script successfully.Do you have any idea how i can run otherwise a python script in qt?
-
wrote on 3 Jul 2019, 06:05 last edited by
i reinstalled python and checked the variables … all fine.
And as i said, when i run the Script with cmd ( cmd -> python.exe "path/createJSON.py") it works perfect -
i reinstalled python and checked the variables … all fine.
And as i said, when i run the Script with cmd ( cmd -> python.exe "path/createJSON.py") it works perfect@NotYourFan
well, have you tried giving QProcess the absolute path to the python file, as you did in your cmd prompt ? -
wrote on 3 Jul 2019, 07:33 last edited by
yes of coruse, like:
void TestWindow::loadFile() QProcess p; QStringList params; params << "createJSON.py"; p.start("C:/development/Python/Python37/python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
-
yes of coruse, like:
void TestWindow::loadFile() QProcess p; QStringList params; params << "createJSON.py"; p.start("C:/development/Python/Python37/python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
@NotYourFan said in Run python script in Qt:
I actually meant the absolute path toI assume createJSON.py is inside your project folder and you start the program via QtCreator but, the created exe is not in your project folder -> so it can't find the python script
-
wrote on 3 Jul 2019, 08:11 last edited by
Sry @J-Hilk i missunderstood.
But i try this also …
like:
void TestWindow::loadFile() QProcess p; QStringList params; params << "C:/development/Qt/ProjektXY/createJSON.py"; p.start("C:/development/Python/Python37/python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
i´m desperately … i dont know what to do, i try everything.
-
Sry @J-Hilk i missunderstood.
But i try this also …
like:
void TestWindow::loadFile() QProcess p; QStringList params; params << "C:/development/Qt/ProjektXY/createJSON.py"; p.start("C:/development/Python/Python37/python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
i´m desperately … i dont know what to do, i try everything.
@NotYourFan since you're on windows, you're using the wrong path separator
-
wrote on 3 Jul 2019, 08:22 last edited by
@J-Hilk whats the Right path separator?
-
@J-Hilk whats the Right path separator?
@NotYourFan
https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
backslash or probably double backslash
-
wrote on 3 Jul 2019, 08:26 last edited byThis post is deleted!
-
wrote on 3 Jul 2019, 08:29 last edited by
okay, thx for your Suggestion.
void TestWindow::loadFile() QProcess p; QStringList params; params << "C:\\development\\Qt\\ProjektXY\\createJSON.py"; p.start("C:\\development\\Python\\Python37\\python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
i tried one time with Backslash and one time with double Backslash … same Problem.
-
@NotYourFan since you're on windows, you're using the wrong path separator
@J.Hilk said in Run python script in Qt:
since you're on windows, you're using the wrong path separator
With Qt / should work on Windows as well, shouldn't it?
-
@J.Hilk said in Run python script in Qt:
since you're on windows, you're using the wrong path separator
With Qt / should work on Windows as well, shouldn't it?
@jsulm I'm unsure
usually yes, but in this case you're trying to start a System program from the OS. Until now, I've always only used it to start an external program, that I shipped with the other Qt installation and therefore was always at the same depth as the main executable or it was on a unix system...
@NotYourFan
have you seen this thread ?
https://stackoverflow.com/questions/15127047/qt-calling-external-python-scriptIt may help you
-
@J.Hilk said in Run python script in Qt:
since you're on windows, you're using the wrong path separator
With Qt / should work on Windows as well, shouldn't it?
With Qt / should work on Windows as well, shouldn't it?
It is really important to use forward slashes for Qt functions, yes. However, the parameter for python (the path to the script) is not evaluated from Qt but from the python interpreter, so here backslashes might be needed.
That gives:
params << "C:\\development\\Qt\\ProjektXY\\createJSON.py"; p.start("C:/Development/Python/Python37/python.exe", params);
Looks funny, though.
p.waitForFinished(-1);
I would use a SLOT here.And for testing: can you just try
python -c 'print "Hello World"'
first? -
@jsulm I'm unsure
usually yes, but in this case you're trying to start a System program from the OS. Until now, I've always only used it to start an external program, that I shipped with the other Qt installation and therefore was always at the same depth as the main executable or it was on a unix system...
@NotYourFan
have you seen this thread ?
https://stackoverflow.com/questions/15127047/qt-calling-external-python-scriptIt may help you
@J.Hilk said in Run python script in Qt:
usually yes, but in this case you're trying to start a System program from the OS
As said, the path for QProcess should work with forward slash. The path for the parameter (the script) probably not.
-
wrote on 3 Jul 2019, 11:03 last edited by
@J-Hilk thank you for the stackoverflow link, i've seen it and try it.
@aha_1980 i testpython -c 'print "Hello World"'
--> i get Nothing. -
@J-Hilk thank you for the stackoverflow link, i've seen it and try it.
@aha_1980 i testpython -c 'print "Hello World"'
--> i get Nothing.@NotYourFan said in Run python script in Qt:
@aha_1980 i test python -c 'print "Hello World"' --> i get Nothing.
How did you test it?
-
wrote on 3 Jul 2019, 11:58 last edited by
put
python -c 'print "Hello World"'
in p.start -
put
python -c 'print "Hello World"'
in p.startwrote on 3 Jul 2019, 12:29 last edited by@NotYourFan just in case, you may want to use Python embedded in your Qt application...
1/31