Python Central HubSign in

Automation and Scripting / Phase 6: Task Scheduling & Deployment

Scheduling Scripts on Windows (Task Scheduler)

Schedule Python scripts on Windows using Task Scheduler with reliable paths.

Key idea

Task Scheduler runs your script in a different environment than your terminal.

Be explicit about:

  • Python executable path
  • script path
  • working directory

Practical advice

  • Use a .bat file that activates venv then runs the script
  • Log output to a file

Example batch file

Text
@echo off
cd /d C:\path\to\project
C:\path\to\venv\Scripts\python.exe my_script.py --source C:\data

Creating a task

Task Scheduler is the built-in Windows tool for running programs at set times. You can use the graphical tool (search for "Task Scheduler") or the schtasks command. The graphical steps are:

  1. Choose Create Task, not Create Basic Task, so that you get all the settings.
  2. On General, give it a name and choose "Run whether user is logged on or not" if the script should run without you signed in. It asks for your password.
  3. On Triggers, add a schedule: daily at a time, at log on, or at start-up.
  4. On Actions, choose "Start a program" and fill in three boxes, which are described below.
  5. On Settings, choose what happens if the task runs too long or is missed.

The three boxes that matter

The environment differs from your terminal: no activated virtual environment, a different working directory, and a shorter PATH. So be explicit.

  • Program/script: the full path to python.exe, for instance C:\path\to\venv\Scripts\python.exe, or the path to your .bat file.
  • Add arguments: the script and its arguments, for example my_script.py --source C:\data.
  • Start in: the folder the script should run from. Without it, relative paths point to C:\Windows\System32, which is the classic cause of "it works by hand but not in Task Scheduler".

Using the batch file from this page keeps all three in one place, so you can run the file by hand to test it. Add logging to the batch file:

Text
@echo off
cd /d C:\path\to\project
echo ===== %date% %time% ===== >> job.log
C:\path\to\venv\Scripts\python.exe my_script.py --source C:\data >> job.log 2>&1
echo exit code %errorlevel% >> job.log

>> job.log 2>&1 appends standard output and errors to the file, and %errorlevel% records whether Python succeeded (0) or failed.

Creating it from the command line

schtasks creates the same task in one line, which is handy for setup scripts:

Text
schtasks /Create /TN "NightlyReport" /TR "C:\path\to\project\run_job.bat" /SC DAILY /ST 09:00

/TN is the name, /TR the program to run, /SC the schedule, and /ST the start time. Run it now to test with schtasks /Run /TN "NightlyReport", and remove it with schtasks /Delete /TN "NightlyReport" /F.

Reading the result

The Last Run Result column shows a code. 0x0 means the program started and exited with success. 0x1 means the program returned an error, which your log will explain. 0x2 means the file was not found, which is normally a wrong path. 0x41301 means the task is still running. If the history tab is empty, enable it in the right-hand pane ("Enable All Tasks History").

Common mistakes

  • Leaving "Start in" empty.
  • Using python instead of the full path, so the wrong Python, or none, runs.
  • A task that only runs when you are signed in, then fails on a locked or logged-out machine.
  • Missing a run because the computer was asleep. Tick "Run task as soon as possible after a scheduled start is missed", and consider "Wake the computer to run this task".
  • Password changes. When your Windows password changes, a saved task needs updating.
  • Mapped network drives such as Z:\. They are not available to background tasks, so use UNC paths (\\server\share).

Check your understanding

quizCheck yourself
  1. A script works in your terminal but fails in Task Scheduler with file not found errors for relative paths. What should you set?

    Show answer

    A — The Start in folder, and ideally use absolute paths — Without a Start in folder the working directory is not your project, so relative paths point somewhere else.

  2. What does >> job.log 2>&1 do in the batch file?

    Show answer

    A — Appends both normal output and error output to job.log — >> appends standard output, and 2>&1 sends the error stream to the same place.

  3. Why use the full path to python.exe in the venv?

    Show answer

    A — Task Scheduler does not activate the venv or share your PATH, so this guarantees the right interpreter and packages — The scheduled task has its own minimal environment. Naming the venv interpreter directly makes sure your installed packages are found.

Was this page helpful?

Let us know how we did