Background
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.
How to run Python from the command line
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
Running Python from the Task Scheduler
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.
How to run a Python script without being logged on
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.
Triggering the script 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.
Originally posted on TheAutomatic.net blog.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.