3

I am trying to automate a python script through the Windows Task Scheduler but its not working. At the end of my python script. two CSV files should be created but they arent.

I tried the following: 1. Copied the address of my python.exe to Program/Script.

C:\Program Files\Python35\python.exe

  1. In the Add arguments, i put the name of my file

Historical Aggregation.py

  1. In the Start in (optional), i put the path of my python script

C:\Users\myname\PycharmProjects\Project1

Am I missing something

Roger Steinberg
  • 1,554
  • 2
  • 18
  • 46

4 Answers4

9

To simplify, we can create a really short .bat file, that will only receive the necessary command to run your python script.

To do so, try this:

Create a executePy.bat file in the same folder than your Python file (C:\Users\myname\PycharmProjects\Project1), with content:

@echo off
"C:\Program Files\Python35\python.exe" "Historical Aggregation.py"

Then, on your task scheduler, simply schedule a test with Program/Script:

"C:\Users\myname\PycharmProjects\Project1\executePy.bat"

Leave Add Arguments and Start In in blank. Now, your task should be ready to run.

Pedro Martins de Souza
  • 1,406
  • 1
  • 13
  • 35
  • that makes sense but what I did should work in theory – Roger Steinberg Dec 10 '18 at 19:37
  • tried the bat file but doesnt work, should i restart my computer? – Roger Steinberg Dec 10 '18 at 19:41
  • Make sure you properly used quotation marks. I posted two different answers and both of them worked for me – Pedro Martins de Souza Dec 10 '18 at 19:48
  • 2
    for future visitors: If you are behind a firewall, make sure you have the permission to schedule jobs. – hm6 Mar 04 '20 at 22:02
  • Hey OP, did these solutions work for you? I'm having the same exact error as you, and none of these are working. – Empyz Mar 15 '22 at 12:30
  • Hey @Empyz, what errors are you meeting? – Pedro Martins de Souza Mar 16 '22 at 13:40
  • Hey @PedroMartinsdeSouza - for me, my task just never runs. The part where it says "last run date" just stays 1999 and it never runs. I know the script works because when I run it in cmd prompt or in spyder, it works. – Empyz Mar 16 '22 at 14:33
  • Addition comment for future visitors: For me, a python program would fully run w/o errors using a batch file from either file manager or command prompt. However, it would fail with an error in one of the library packages when run from scheduler. It turned out that Task Scheduler runs using the system "PATH", not the user "PATH". Therefore, make sure the system path has all the python subdirectories that the user path does. (that fixed my issue) – JerryD Nov 21 '22 at 21:13
4

I had a very similar issue, and solved it in a different way. Here my step by step guide:

  1. Transform the python script to an .exe, using in the DOS cmd prompt the command:

    pyinstaller -- onefile [name of the file.py]

  2. Place the CSV file that you want to update in the same folder as the .exe file created

  3. Create a basic task on Windows Scheduler, with the following properties:

    • General - select

      • Run whether user is logged on or not
      • add the PC password. For my PC, use the user name DESKTOP-M40FS79\dario and the PC password
      • Run with highest privileges
    • Triggers – select

      • Daily
      • Repeat task every 30 minutes
      • Stop task if it runs longer than 15 minutes
    • Actions

      • Under Program/ Script insert the path to your .exe file, for instance, C:\Pythondata\dist\test.exe
      • Under Start in (optional) insert the path to the directory where the CSV and .exe files are, for instance C:\Pythondata\dist\
    • Conditions - select

      • Start the task only if the computer is on AC power, and make sure you have connected the power
      • Wake the computer to run this task
    • Settings – leave the default options

  4. Save the task by inserting the password

  5. Leave the task status on “Ready”

Good luck!

tshimkus
  • 1,173
  • 2
  • 17
  • 24
Dario
  • 41
  • 1
2

Another approach would be to set fields as:

  1. Program/Script - your python path (with quotation marks):

"C:\Program Files\Python35\python.exe"

  1. Add arguments - full file name of the script, including it's path (with quotation marks):

"C:\Users\myname\PycharmProjects\Project1\Historical Aggregation.py"

Pedro Martins de Souza
  • 1,406
  • 1
  • 13
  • 35
0

Probably you have some spaces in the directory path, try remove them or find a way to handle them in the path (I think task scheduler does not receive updates as powershell or CMD does...)

Atom94
  • 1
  • 4