How to force Python.exe to stay open when launched from Windows Task Scheduler
I am running a .py file from Windows Task Scheduler. For most files, this works fine:
- Create a new task in TS
- In TS, on the "Actions" tab in the "Program / script:" section, specify the path to python.exe (example: C: \ Users \ User \ Python.exe)
- In TS under "Add Arguments" add the path to the .py file in quotes (Example: "C: \ Path \ To \ Py \ pythonfiletorun.py")
When it works correctly, Python.exe appears within a second of a second, launches the program and disappears.
However, I have one .py file that doesn't work. When I try to run it from QT Console it works fine. When I use TS process it fails. And Python.exe closes down so quickly that I can't see what is wrong.
I tried to convert everything to a batch file, so "Program / script" becomes "cmd" and "Add Arguments" becomes: / k "C: \ Path \ To \ batchfile.bat", which then contains paths and instructions.
This opens a command prompt (and keeps opening it), but from here, the main command prompt opens the python.exe command prompt, which starts and closes just as quickly.
I have tried putting / k in different places to no avail.
I've also tried putting an input qualifier at the end of the .py script (Ex: input ("Press any key ...") in the hopes that the Python.exe command will stay open, but it doesn't.
Ultimately, I need the Python.exe command to stay open so I can figure out what is wrong.
source to share
Ok, so the approach below worked (thanks everyone for their input).
First, create a batch file that reads:
start C:\Users\Path\To\Python.exe C:\Users\Path\To\PyFileToRun.py
Go to your save location and double click to make sure it works. Once that works, convert it with "-i" between the Python.exe path and the .py path file as follows:
start C:\Users\Path\To\Python.exe -i C:\Users\Path\To\PyFileToRun.py
Now go to TS and insert into "Program / script":
C:\Users\Path\To\BatchFile.bat
Leave the "Arguments" field in TS blank.
Now run the TS task and it should run and leave Python.exe open so you can investigate the problems.
Thanks again
source to share