Solved Run python script in Qt
-
@NotYourFan
https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
backslash or probably double backslash
-
This post is deleted! -
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.
-
@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
-
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? -
@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.
-
-
@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?
-
put
python -c 'print "Hello World"'
in p.start -
@NotYourFan just in case, you may want to use Python embedded in your Qt application...
-
@NotYourFan It doesn't work like this. You have to put the command as first parameter to start() and all parameters for the command as parameter list.
-
-
@jsulm i readed in the Qt Doc.
@Gojir4 you are right.
@Pablo-J-Rogina thx for your Suggestion … but i should / want to do it with QProcess … -
Hi,
@NotYourFan just in case, did you try to compare the environment variables of your QProcess object and the one you have when running e.g. from a terminal ?
@Gojir4 You are right about that specific overload, but the documentation also states that this overload should be avoided if possible.
-
@SGaist My bad. Thanks for clarification !
-
@NotYourFan
I am lost as to where you are now.Assuming it's still crashing --- is that right? --- surely you want to break it down to simplify now. Start with:
params << "-V"; p.start("C:\\development\\Python\\Python37\\python.exe", params); p.waitForFinished(-1); QString p_stdout = p.readAll();
What exactly happens?
-
@JonB, i get a error Message: ….
ModuleNotFoundError: No module named 'encodings'
-
@NotYourFan
OK! So, you can forget about yourcreateJSON.py
script and anything it does.I don't know why you get this when spawning from Qt but not when running your
python.exe
directly from a command prompt. Either it's something to do with it sending output to a pipe you are reading from, or it's to do with your general Python environment being different between the two situations.You want now to Google for
python ModuleNotFoundError: No module named 'encodings'
. There are posts/suggestions. Looks to me like it might be to do with whether you are using a virtual environment or not, but I'm sure one of the hits will sort you out.... -
@JonB I can imagine that the Environment inside Qt Creator is different (e.g. PATH) and therefore other (incompatible) executables and libraries are found.
@NotYourFan: You should carefully check the Run Environment variables inside Qt Creator, especially PATH. That's all I can say for now.
Regards