How to run Python scripts via Task Scheduler?

How to Schedule a Python File with Task Scheduler in Windows 10?

I want to schedule a .py file using Task Scheduler on Windows 10, but every time I try to run it, a window pops up asking, “How would you like to open the program?”

I attempted to convert my .py file into an .exe, but due to the complexity of my code, the .exe approach caused issues. Now, I want to schedule the .py file directly. How can I successfully set up and run a Python script using Task Scheduler without any errors?

System: Windows 10
Python Version: 3.5.2

I’ve worked with task scheduler python quite a bit, and here’s a direct way to get started.

To schedule a Python script using Windows Task Scheduler, follow these steps:

  1. Open Windows Task Scheduler and click Create Basic Task in the Actions pane on the right.
  2. The wizard will guide you through naming the task, setting the trigger (when it runs), and specifying the action (what program to run).
  3. On the Action tab, don’t point directly to your script. Instead, specify the Python executable as the program to run and provide the script path as an argument.

Here’s an example if your script is at E:\My script.py:

C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"  

If you’re unsure where Python is installed, use this Python snippet to find it:

import sys  
print("Python EXE Path: ", sys.executable)  

This ensures the Task Scheduler uses the correct Python interpreter, especially helpful if you have multiple Python versions installed. Add arguments as needed after the script path.

Great points, @miro.vasil! Let me add something I’ve found handy in task scheduler python setups."

Instead of running Python directly in Task Scheduler, you can create a batch file to simplify things. This makes the process more manageable, especially if you have to tweak things later.

  1. Create a .bat file, like run_script.bat, with this content:
@echo off  
C:\Path\to\python.exe E:\My script.py  
  1. In Task Scheduler, set the Action to run the batch file instead of directly pointing to Python.

Why do this? It avoids hardcoding paths repeatedly and makes adjustments simpler if your script or Python version changes. Plus, it ensures Task Scheduler consistently executes the right Python interpreter for your script.

@miro.vasil and @Rashmihasija made great points! Now, here’s a tip for those of you using virtual environments in your task scheduler python workflows."*

If you’re working with a Python virtual environment, it’s crucial to use the environment’s Python executable for the task. This ensures the script runs with the correct dependencies.

  1. Activate your virtual environment with:
path\to\venv\Scripts\activate  
  1. In Task Scheduler, set the Action to the virtual environment’s Python executable, followed by your script path. For example:
C:\path\to\venv\Scripts\python.exe "E:\My script.py"  

This method ensures Task Scheduler uses the dependencies you’ve configured in the virtual environment. It’s perfect for complex projects where dependency management is key.

By combining these approaches—direct execution, batch files, or virtual environments—you’ll master task scheduler python setups effortlessly.