Sometimes we need to run python scripts periodically to get some work done, running them on some specific time interval. One way is to run it manually every time. But there is a more easy, smarter way to do this. We can save our time by automating this process. We can use corn job on Linux and Mac, but on Windows, We use Windows Task scheduler to run some programs periodically on some regular interval.
Here, I will show you how you can run a python scripts on some regular basis automatically on Windows, without manually running them every time.
For simply running the python using your default version of installed python, you can skip this, but if you have multiple python versions installed then you can search where python is installed on your system, and also what versions, using below command:
This will give result like below screenshot.
Here, you can see I have two versions of python installed, python 2.7 and python 3.5. In case, you also have multiple versions of python installed, you need to decide on which version your python script will run best, because when we will schedule the process we need to supply the python.exe, so we need to know the full path of python.exe version you decided.
Now there could be two scenarios:
- You want to run one script without any Virtual environment
- You have a python script that runs inside a virtual environment
Run Python Script Without Virtual Environment
This is very simple to do. Here we need to set the python.exe as the main program and the path of the file as argument.
For that First open Task Scheduler and then Select “Task Scheduler Library” and then right click on it. Select Create Basic Task from the options.
Give it a name and description. Click on Next. It will ask for the trigger frequency. Select as per your requirement. For action select “Start a program”.
For program/script select the python.exe file’s full path as you found in previous step. If you don’t have multiple versions of python installed, then simply use python.exe. For argument, give the path of your python script file. If you are writing to any file from this script using relative path, then make sure you give your python script’s folder path as value of Start in (optional). Now click next and Finish.
You will notice when ever this program runs using task scheduler, a command line window pops up and closes automatically. This is not any issue, but sometimes its very inconvenient especially when you are working on that machine. We can set this job to run without showing us command line unnecessarily. For that, just right click on that task, select properties and you will see that in General tab’s security options, “Run only when user is logged on” is selected. Change it to “Run whether user is logged on or not“. Click on OK, and it will ask for your system’s password. Enter password and hit OK. This will not show the command line and your job will run in background.
Schedule Python Script To Run Inside Virtual Environment
In this case, Process is same as scheduling a normal python script, but we have one extra step to do, as we are on Windows Operating system, we will need to create a BAT file. We will add logic of executing the python script in this bat file, and then this bat file will be used in Windows Task Scheduler as the file to execute. Good thing is that you don’t need to know the path of python.exe, because as this uses virtual environment, it will use python.exe of the virtual environment.
Lets’ create a new bat file. Open your favourite text editor and put this code in it:
call .\myenv\Scripts\activate python myscript.py call deactivate
This code first activated the virtual environment. Then runs the python script and when process is completed, it deactivates the virtual environment.
Now create a new task. You can refer above for steps for creating new task. Set the triggers as per your requirement. For Actions, select Start a program option, and in this for program/script select the BAT file you just created. For argument, if your python script requires any argument, then write that here, otherwise keep it empty. Make sure you give your python script’s folder path as value of Start in (optional). Now click next and Finish. You job is created and scheduled as per your requirement.
This will also open the windows command line every time the task will run. To make it run without showing the command line, you can set “Run whether user is logged on or not” option.