3

I am running a .py file from Windows Task Scheduler. For most files this works fine as follows:

  1. Create new task in TS
  2. In TS on the "Actions" tab under "Program/script:" specify the path to python.exe (Example: C:\Users\User\Python.exe)
  3. In TS in the "Add arguments" section, add the path to the .py file in quotes (Example: "C:\Path\To\Py\pythonfiletorun.py")

When this works correctly, Python.exe pops up for a split second, runs the program and vanishes.

However, I have one .py file that isn't working. When I try to run it from QT Console it works fine. When I use the TS process it fails. And the Python.exe closes so fast I can't see what is wrong.

I tried converting everything to a batch file so "Program/script" becomes "cmd" and "Add arguments" becomes: /k "C:\Path\To\batchfile.bat" which then houses the paths and instructions.

This opens a command prompt (and keeps it open) but from here the main command prompt opens the python.exe command prompt which runs and closes just as fast.

I tried placing the /k in various places to no avail.

I also tried putting an input qualifier at the end of the .py script (Ex: input("Press any key to...") in hopes that would cause the Python.exe command to stay open but it doesn't.

Ultimately, I need the Python.exe command to stay open to see what is wrong.

GC123
  • 323
  • 5
  • 15
  • Why use TS to debug an incorrect script? Can't you just run the batch file without TS? – Thomas Weller Mar 22 '17 at 19:38
  • @ThomasWeller The script actually works fine when run from iPython QT Console it is when it is run from TS that it fails. The idea behind the batch file was it would allow the /k argument in to stop the Python.exe. Little out of my knowledge base here found this online so tried it. – GC123 Mar 22 '17 at 19:42
  • how about adding `-i` to your args to force interactive mode after running (or crashing) – Aaron Mar 22 '17 at 19:45
  • If you're using cmd's built-in `start` command in the batch file, note that it defaults to creating a new console. Either don't use it or add the options `/b /w` to have it use the current console and wait. Or just run it without a batch file, e.g. something like `cmd.exe /k ""path\to\python.exe" "path\to\script.py""`. Aaron's suggestion to use Python's `-i` option should also work. – Eryk Sun Mar 22 '17 at 19:46
  • @Aaron not familiar with this. I assume it would go into TS? Does the placement matter? – GC123 Mar 22 '17 at 19:47
  • @GC123 it's an optarg for python... `C:\some\folder>python script.py -i`. call `>python -h` from a cmd window for all options – Aaron Mar 22 '17 at 19:48
  • I don't mean an iPython Qt Console. I'm talking about running the batch file in `cmd`. – Thomas Weller Mar 22 '17 at 19:56
  • Let's say I don't use cmd and instead just want to put the path to Python.exe and the .py file into TS. How would I stop python.exe (or get the problems it prints for a fraction of a second)? Most of the responses seem to focus more on cmd which I don't know well and the attempts to freeze the cmd still don't stop the python.exe from closing (or I am doing it wrong). – GC123 Mar 22 '17 at 19:59
  • 1
    maybe you can use the `getch()` function in the `mscvrt` module. See [**_Python read a single character from the user_**](http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user) to pause until a character is pressed. – martineau Mar 22 '17 at 20:53
  • 1
    @Aaron, the `-i` option comes before the script name, e.g. `"path\to\python.exe" -i "\path\to\script.py"`. – Eryk Sun Mar 22 '17 at 20:55
  • @GC123 I would expect Task Scheduler to work in a similar manner to cmd, so if you can get it to do what you want there, you should be able to replicate it with TS – Aaron Mar 22 '17 at 20:55
  • @eryksun good catch, I should have realized that sooner. – Aaron Mar 22 '17 at 20:57
  • @martineau, unless the OP is handling all possible exceptions, then using a -i REPL or a parent `cmd /k` shell is the way to handle this. The -i option suffices for everything except a crash. If Python is crashing (e.g. a Windows access violation exception), then a `cmd /k` shell is the way to go. – Eryk Sun Mar 22 '17 at 21:01
  • Can you post a snippet of this .py code? Is it connecting to a server? Calling files at relative paths? Task scheduler uses Admin and not local user so read/write on files should be granted. – Parfait Mar 23 '17 at 00:54
  • Pipe the output to a file and see what the error is. Debugging 101. – David Heffernan Mar 23 '17 at 05:19

1 Answers1

4

Ok so the approach below worked (thanks to all for their input).

First, create a batch file that says:

start C:\Users\Path\To\Python.exe C:\Users\Path\To\PyFileToRun.py 

Go to where this is saved and double click to ensure it works. Once this works, recopy it with the "-i" between the Python.exe path and the .py file path like so:

start C:\Users\Path\To\Python.exe -i C:\Users\Path\To\PyFileToRun.py 

Now go to TS and in "Program/script" insert:

C:\Users\Path\To\BatchFile.bat

Leave the "Arguments" field in TS blank.

Now run the TS task and it should run and leave the Python.exe open so you can explore the problems.

Thanks again all

GC123
  • 323
  • 5
  • 15
  • 3
    Given everything that was said in the comments above, why are you still using a batch file and the `start` command? The task scheduler can simply run either `"C:\Users\Path\To\Python.exe"` with arguments set as `-i "C:\Users\Path\To\PyFileToRun.py"`, or instead run `cmd.exe` with arguments set as `/k ""C:\Users\Path\To\Python.exe" -i "C:\Users\Path\To\PyFileToRun.py""`. – Eryk Sun Mar 23 '17 at 03:32
  • @eryksun I mainly just fumbled around until I got something that worked (answer above) and stopped looking for other solutions. – GC123 Mar 23 '17 at 17:54
  • This was a huge help. I was able to figure out the problem. Thank you! – TravisVOX May 31 '19 at 04:30