Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a .py file on Task Scheduler in Windows 10

Tags:

python

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

like image 685
Stef Avatar asked Jun 23 '17 17:06

Stef


People also ask

Can you schedule a Python script?

One good approach is to schedule these scripts as per your need; daily at 5 PM or weekly once on Sunday 12 AM etc. There are a number tools available at your disposal such as — schedule, apscheduler, python-crontab, apache-airflow, etc. that you can use to schedule your Python jobs .

How do I automate a Python script in Windows?

Automate Python Script Using Windows Task Scheduler Give your task a name. Go to the “Tiggers” tab, change the settings to Daily, and make sure recur every 1 day since we want the bot to run every day. Also, make sure the Start time is in the past, otherwise, it won't run until that time comes.


Video Answer


3 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:\Python27\ArcGIS10.2\python.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("\n\nPress 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.

like image 73
Dragon Avatar answered Oct 18 '22 07:10

Dragon


You should set in the Action tab:

  • in programs, the path to your python.exe: for instance "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • in arguments, the full path to your file including the extension: for instance "C:\Users\Me\Desktop\mypythonsrcipt.py"
  • for start in: leave empty

If this doesn't work, try :

  • in programs, the path to your python.exe: for instance "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • in arguments, your filename with the extension: for instance "mypythonsrcipt.py"
  • in start in, the folder of you script : for instance "C:\Users\Me\Desktop\"

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.

like image 39
MagTun Avatar answered Oct 18 '22 06:10

MagTun


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.

like image 10
ToCarbajal Avatar answered Oct 18 '22 05:10

ToCarbajal