0

The following code is failing to launch the python command line.

QProcess *myProcess = new QProcess(this);
myProcess->start("\"C:\\Program Files\\Python27\\python.exe\"");

If I replace python27 with (for example)

myProcess->start("\"C:\\Program Files\\Notepad++\\notepad++.exe\"")

notepad opens. Why is my program able to launch notepad but not Python Command Line?

I tried using startDetached() as suggested here but that didn't make a difference.

QProcess::Error() gives me error 5: unknown error.

t020608
  • 333
  • 2
  • 3
  • 12
  • 1
    What exactly do you want to do with python? python.exe is the interpreter binary, so if you want to run a specific .py script you need to pass it as argument to python but from the command line(run cmd.exe line instead of python with 'python script.py' as argument) – Lucian Jul 24 '17 at 14:14
  • @t020608 Are you sure `python.exe` is running normally ? like double click on it ? – Farhad Jul 24 '17 at 14:14
  • @Lucian for now I just wanted to see if I could open python. In the long run I want to execute python scripts created in my Qt GUI (ideally with the ability to monitor progress, and pause the script). Should I use the command line then? – t020608 Jul 24 '17 at 14:30
  • I just ran your code in Qt5.5 and python 3.6. With `myProcess->start(...)` nothing happened, but `QProcess::startDetached(...)` worked. Are you sure that the path is correct? Maybe python is installed in `C:\Program Files (x86)` and not in `C:\Program Files`. Are you sure you did not make any spelling errors? – pschill Jul 24 '17 at 14:47
  • @pschill i'm sure that I got the path correct because I am able to launch the python using `system("start C:\\Python27\\python.exe");` (i changed the location of Python27 folder btw) – t020608 Jul 24 '17 at 16:15

2 Answers2

0

If you just want to use the 'python console' you must use cmd.exe application from windows
You must have python in PATH so the windows console will know where to take it from.
So, you can try: QProcess::startDetached("cmd", "python")..see more specific syntax details here

Lucian
  • 874
  • 11
  • 33
  • i tried this but opening the command prompt doesnt work either. I've tried QProcess::startDetached("cmd.exe") as well with the full path as argument ("C:\Windows\system32\cmd.exe"). any ideas why it doesn't work for me when it worked for [this person](https://stackoverflow.com/questions/10363918/qt-cant-launch-windows-console-with-qprocess) ?? – t020608 Jul 24 '17 at 15:34
  • @t020608 if you are on 64 bit, u may try "C:\Windows\SysWOW64\cmd.exe" .... check https://stackoverflow.com/questions/39732079/qt5-calling-bat-file-with-qprocessstartdetached-doesnt-find-program-in-syst – HazemGomaa Jul 24 '17 at 20:44
0

It seems I've misunderstood what happens when you launch a command line. I was expecting the python command line or command prompt window to open.

It turns out that if I just pass my commands as arguments start() like so:

 myProcess->start("cmd.exe /C python C:\\Users\\SP4\\Desktop\\helloworld.py");

Command prompt runs my python script and I get the output ("Hello World") using:

QString output = myProcess->readAllStandardOutput();

All this happens in the background and you can't actually see a command line window open and print out "Hello, World".

Please correct me if I've misunderstood something.

t020608
  • 333
  • 2
  • 3
  • 12