Running Python 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 extracting data from a database on an automated, regular basis is a common need at many companies.
Before we go into how to schedule a Python script to run, you need to understand how to run Python from the command line. To open the command prompt (command line), press the windows key and type cmd into the search box.
Next, 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:
python C:\Users\cool_python_script.py
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:
C:\Anaconda3\python.exe cool_python_script.py
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.
python C:\Users\cool_python_script.py
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.
Select the “Run only when user is logged on” option if you’re just running a task on a computer where you’ll be logged on when you need it to run.
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! Check out my other Python posts here, or click here to get more great resources on learning Python and R.
Very excited to announce the early-access preview (MEAP) of my upcoming book, Software Engineering for…
Ever had long-running code that you don't know when it's going to finish running? If…
Background If you've done any type of data analysis in Python, chances are you've probably…
In this post, we will investigate the pandas_profiling and sweetviz packages, which can be used…
In this post, we're going to cover how to plot XGBoost trees in R. XGBoost…
In this post, we'll discuss the underrated Python collections package, which is part of the…