Being able to run Python scripts from the Windows Task Scheduler is a really useful capability. It allows you to run Python in production on a Windows system, and can save countless hours of work. For instance, running code like this previous article about scraping stock articles on an automated, regular basis, could come in handy as new stock articles are posted.
Before we go into how to schedule a Python script to run, you need to understand how to run Python from the command line. Just press the windows key and type cmd into the search box to make the command prompt come up.
Suppose your python script is called cool_python_script.py, and is saved under C:\Users. You can run this script from the command prompt by typing the below line:
If you get an error saying “‘python’ is not recognized as an internal or external command”, then Python is probably not in your environment path. You can handle this in one of two main ways, but they both involve you figuring out where the Python executable is stored on your machine. You should be able to do this by searching for python.exe and opening the file location. Once the python.exe shows up in your search results, just right click and click “Open file location.”
This should pull up the directory where python.exe is installed. Then you would use the full path (with this directory) to python.exe in your command prompt call. For example, if python.exe is stored at C:\Anaconda3, you would type:
The other solution is to add this location to your system PATH variable. For doing that, see this reference. Then, you should be able to type the command from above without errors.
Once you know your Python script works from the command prompt, you can create a task in the Windows Task Scheduler to make it run at specific times and dates. Pressing the windows key, followed by typing “task scheduler” should bring the Task Scheduler up. Once it’s open, click on “Action”, and then press “Create Task.”
After this, you will see a place where you need to input the name of your task.
You will also see where you can select “Run whether user is logged on or not.” If you need to run a Python script in production where you won’t necessarily be logged on all the time, then you should select the “whether user is logged on or not” option. As a note, however, you will need to have the appropriate permissions for this to work for you. This generally means you’ll need to have batch job rights. If you’re an administrator on the machine you’re creating the task, then this shouldn’t be a problem.
If you’re just running a task on a computer where you’ll be logged on when you need it to run, then you can select the “Run only when user is logged on” option.
The next step is to select the “Triggers” tab.
You should get a box that pops up where you can select what dates / times you need your Python script to run. After you’ve made your selections, you can go to the “Actions” tab, where you’ll create a new action. Clicking “New” brings up the box below.
Here, you’ll input “python” into the “Program/Script” box, and type in the name of your script into the “Add Arguments (optional)” box.
After this, you should be all set! Just press “OK”, and you’re done.
That’s it for now, but feel free to check out my other posts from the links on the left side, or the related articles below.