Scheduling a .py file on Task Scheduler in Windows 10

Question:

I already tried to convert my .py file into .exe file. Unfortunately, the .exe file gives problems; I believe this is because my code is fairly complicated.
So, I am trying to schedule directly my .py file with Task Scheduler but every time I do it and then run it to see if works, a window pops up and asks me how I would like to open the program?-.-

Does any of you know how I can successfully schedule my .py file with Task Scheduler? Please help, thanks

Windows 10
Python 3.5.2

Asked By: Stef

||

Answers:

Creating the exe should be the best method. But if you want to run it with the task scheduler you can do it in this way:

  1. Launch Window’s Task Scheduler
  2. Look for the The Actions pane(on the right) it has the Create Basic Task action. Click on it.
  3. This will open a wizard where you will define the name of your task, the trigger (when it runs), and the action (what program to run).
    Action tab is where you specify the name of your Python script to run as well as any arguments to the script.

To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of Python is used in mixed environments (64bit or 32bit), it is recommended that you run the Python executable with the name of your Python file as an argument to the executable.

Suppose the script you want to run is E:My script.py. Instead of running the script directly, instruct the task scheduler to run python.exe with the script as an argument. For example:

C:Python27ArcGIS10.2python.exe “E:My script.py”

The location of python.exe depends on your install. If you don’t know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location of python.exe as well as other information about your Python environment.

import sys
import platform
import imp

print("Python EXE     : " + sys.executable)
print("Architecture   : " + platform.architecture()[0])
print("Path to arcpy  : " + imp.find_module("arcpy")[1])

raw_input("nnPress ENTER to quit")

After determining the location of python.exe, this is what is entered in the Action panel of the task scheduler:
enter image description here

If there are additional arguments (parameters) to your script, provide them after the path to your script. Hope this helps.

Answered By: Dragon

The script you execute would be the exe found in your python directory
ex) C:Python27python.exe

The “argument” would be the path to your script
ex) C:PathToScript.py

So think of it like this: you aren’t executing your script technically as a scheduled task. You are executing the root python exe for your computer with your script being fed as a parameter.

Answered By: Connor

You should set in the Action tab:

  • in programs, the path to your python.exe: for instance "C:UsersMeAppDataLocalProgramsPythonPython36python.exe"
  • in arguments, the full path to your file including the extension: for instance
    "C:UsersMeDesktopmypythonsrcipt.py"
  • for start in: leave empty

If this doesn’t work, try :

  • in programs, the path to your python.exe: for instance "C:UsersMeAppDataLocalProgramsPythonPython36python.exe"
  • in arguments, your filename with the extension: for instance
    "mypythonsrcipt.py"
  • in start in, the folder of you script : for instance "C:UsersMeDesktop"

Also each time I modify a task, the user account in the General tab is switched to Medium Mandatory Level. So I have to reopen the taks and set the user account back to my username: (cf. this question)

enter image description here

If you still can’t run your script, go in Event Log, Applications and Service Log/Microsoft/Windows/TaskScheduler/Operational (right click it to enable it) and look for the errors.

Answered By: MagTun

I almost lost my hair over this. I always got 0x1 as the result of doing what is described above. A long experienced Windows admin told me this:

Create a batch file:

SET logfile="C:Reportsbatch.log"
@echo off
@echo Starting Script at %date% %time% >> %logfile%
"C:Program Files (x86)Microsoft Visual StudioSharedPython36_64python.exe" "C:Userssys_wwwsourcereposhardwareReportAssembler.py"
@echo finished at %date% %time% >> %logfile%

Then provide the batch file in the action part of the task config. One thing to also take care of that all the files written during the runtime of the python program can actually be accessed by the user executing the script.

I tried using the script as a parameter and the python exe in programm/script. First I go the error “Windows Scheduled Tasks not running”.
Then after some configuring around I got the error 0x1, which told me absolutely nothing.

Answered By: user637338

This worked for me, you could try with the Action pane:
The Action pane

Please note that the path of py file in the double quotes, add arguments: $(Arg0)

Check “Run with highest privileges” in the General tab, and it’d better to choose “Run a new instance in parallel” in drop down menu in Settings tab.

After all, you could set the schedule for task in Triggers tab to check the effect of changes (remember choose “Run whether user is logged on or not” in the General tab).

Answered By: Ozcar Nguyen

For some reason, the windows task scheduler starts the python.exe in an environment, where they fail at import-module statements. I had to use a workaround using the CMD.exe and pass the command to run the python script in the given folder.

Use the program

CMD

then the options tab:

/c python main.py

and fill in the source location in the tab Start in:

C:Users...

Not sure what’s the reason for that. The previously described solutions all did not work for me.

Answered By: Maik Holzhey

This absolutely worked for me . I am using windows 10 professional edition and it has taken me almost 6 months to get this solution.Thanks to the suggestion made above.

I followed this suggestion and it worked right away and smoothly. All I did was to instruct the scheduler to run python.exe with my script as an argument just as explained by this fellow below

This what I did Suppose the script you want to run is E:My script.py. Instead of running the script directly, instruct the task scheduler to run python.exe with the script as an argument. For example:

C:Python27ArcGIS10.2python.exe

“E:My script.py”

The location of python.exe depends on your install. If you don’t know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location of python.exe as well as other information about your Python environment.

Answered By: Emmy

Dragon’s answer works fine as long as your script not takes much time to finish.

I used a little different approach in the Windows task scheduler:

In Program/script textbox you set the path to Python executable (in my case is inside the virtualenv folder).

Add arguments = Just the name of your Python script.

Start in = The full path of your Python script (without the name.py).

enter image description here

This way the script runs, the console stays open and waits until the end.

Answered By: ToCarbajal

The scheduler with a batch file worked for me, but in addition to the full path of a particular python.exe as described above, you may also need/want to start a conda environment for all the other python packages. This line can be added to the batch file:

call C:ProgramDataAnaconda3condabinconda.bat activate myenv

ie note the syntax for conda activate from within a batch file is a little different. It was also necessary first to run

conda init cmd.exe

to set up windows command terminal for the use of conda.

Also if setting up to run on a remote machine note there is a command line interface for the scheduler, so you don’t even need an RDP session to the remote machine, but can set it all up in an ssh session alone:

schtasks /create /SC ONCE /TN <taskname> /TR C:fullpathtoscheduledtask.bat /ST <hh:mm>
Answered By: J B
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.