简体   繁体   中英

How to make Python.exe stay open when running from Windows Task Scheduler

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM