Как запускать скрипты python в windows по расписанию

In this article we are going to schedule a python script using a windows task scheduler i.e. make it launch automatically at a certain time or after a certain time period. Before starting we need to know the following point Python Script It is a bunch of python code meant

In this article, we are going to schedule a python script using a windows task scheduler, i.e. make it launch automatically at a certain time or after a certain time period.

Before starting we need to know the following point:

  • Python Script: It is a bunch of python code meant to be directly executed by the user from the command line. Most of these are automation scripts, meant to automate certain tasks. These usually contain a few lines of code.
  • Windows Scheduler: Windows Task Scheduler is a Microsoft Windows component or program which gives the user ability to schedule certain scripts or programs and automatically launch them at a certain time. It is used majorly to automate certain tasks like daily planners, monthly data scraping, etc.

Let’s implement with Step-wise:

Step 1: Make a python script

First, we’ll make the python script that we want to schedule. If you have already made the script, skip this section. If not, you can make your own python script or follow mine.

Here we made a python script that fetches a random inspirational quote image from a folder where I’ve saved a couple of them and displays it. You’ll need the following python packages/modules: tkinter, PIL, os, random.

Python3

from tkinter import *

from PIL import ImageTk, Image

import os

import random

name = random.choice(os.listdir(

    "C:\Users\NEERAJ RANA\Desktop\quotes_folder\"))

file = "C:\Users\NEERAJ RANA\Desktop\quotes_folder\" + name

root = Tk()

canvas = Canvas(root, width=1300, height=750)

canvas.pack()

img = ImageTk.PhotoImage(Image.open(file))

canvas.create_image(20, 20, anchor=NW, image=img)

root.mainloop()

Remember to change the folder path according to the folder location on your system.

Now, there are two ways to schedule the script. The first method involves making a batch file, the other does not. If you don’t want to make a batch file, you can skip stepping 3.

Step 2(Optional): Make a batch file

A batch file is used to execute commands with the command prompt. It contains a series of commands in plain text you would normally use in the command prompt and is generally used to execute programs.

We are going to make a batch file to run our python script.

  1. First, open up any text editor.
  2. Next, enter the following lines in an empty file:
"C:Python38python.exe" "C:UsersNEERAJ RANADesktopGFG_Articlesschedulerquote.py"
pause

The first string is the path to the python executable on your machine and the second one is the path to your saved python script. If you don’t know the path to your python executable, open up a command prompt and enter the following command: where python.

where command

If you see multiple paths, use any one of them. Also, change the path to your saved python script according to your system. The pause command is used to suspend the processing of the batch program and wait for the user’s input. And lastly, save this file with a .bat extension.

Step 3: Scheduling the script

  • Open up the Task Scheduler application. It should look something like this:

  • Click “Create Basic Task…” in the Actions tab on the right.
  • Enter a suitable name and description to the task in the given fields and click next.
  • Choose the frequency with which you want to want your task to be executed. You can choose between daily, weekly, monthly, etc and then click next.
  • Choose the start date and time when you want the task to be triggered. Also, choose after how many days you want the task to repeat/recur and click next.
  • Choose “Start a program” in “What task do you want to perform?” and click next.
  • If you made a batch file in step 2, just enter the path to the batch file in the program/script section. If you skipped step 2, enter the path to the python executable in the program/script section and enter the path to your python script file in the “Add arguments (optional)” section.
  • If your script has some dependencies installed in some folder, like browser executable, you can include the folder path in the “Start in (optional)” section and click next.

If you made a batch file

If you didn’t make a batch file

  • 8: Lastly, check all the entered values and click finish.

Now, at your chosen time, your script will execute.

In this article, we are going to schedule a python script using a windows task scheduler, i.e. make it launch automatically at a certain time or after a certain time period.

Before starting we need to know the following point:

  • Python Script: It is a bunch of python code meant to be directly executed by the user from the command line. Most of these are automation scripts, meant to automate certain tasks. These usually contain a few lines of code.
  • Windows Scheduler: Windows Task Scheduler is a Microsoft Windows component or program which gives the user ability to schedule certain scripts or programs and automatically launch them at a certain time. It is used majorly to automate certain tasks like daily planners, monthly data scraping, etc.

Let’s implement with Step-wise:

Step 1: Make a python script

First, we’ll make the python script that we want to schedule. If you have already made the script, skip this section. If not, you can make your own python script or follow mine.

Here we made a python script that fetches a random inspirational quote image from a folder where I’ve saved a couple of them and displays it. You’ll need the following python packages/modules: tkinter, PIL, os, random.

Python3

from tkinter import *

from PIL import ImageTk, Image

import os

import random

name = random.choice(os.listdir(

    "C:\Users\NEERAJ RANA\Desktop\quotes_folder\"))

file = "C:\Users\NEERAJ RANA\Desktop\quotes_folder\" + name

root = Tk()

canvas = Canvas(root, width=1300, height=750)

canvas.pack()

img = ImageTk.PhotoImage(Image.open(file))

canvas.create_image(20, 20, anchor=NW, image=img)

root.mainloop()

Remember to change the folder path according to the folder location on your system.

Now, there are two ways to schedule the script. The first method involves making a batch file, the other does not. If you don’t want to make a batch file, you can skip stepping 3.

Step 2(Optional): Make a batch file

A batch file is used to execute commands with the command prompt. It contains a series of commands in plain text you would normally use in the command prompt and is generally used to execute programs.

We are going to make a batch file to run our python script.

  1. First, open up any text editor.
  2. Next, enter the following lines in an empty file:
"C:Python38python.exe" "C:UsersNEERAJ RANADesktopGFG_Articlesschedulerquote.py"
pause

The first string is the path to the python executable on your machine and the second one is the path to your saved python script. If you don’t know the path to your python executable, open up a command prompt and enter the following command: where python.

where command

If you see multiple paths, use any one of them. Also, change the path to your saved python script according to your system. The pause command is used to suspend the processing of the batch program and wait for the user’s input. And lastly, save this file with a .bat extension.

Step 3: Scheduling the script

  • Open up the Task Scheduler application. It should look something like this:

  • Click “Create Basic Task…” in the Actions tab on the right.
  • Enter a suitable name and description to the task in the given fields and click next.
  • Choose the frequency with which you want to want your task to be executed. You can choose between daily, weekly, monthly, etc and then click next.
  • Choose the start date and time when you want the task to be triggered. Also, choose after how many days you want the task to repeat/recur and click next.
  • Choose “Start a program” in “What task do you want to perform?” and click next.
  • If you made a batch file in step 2, just enter the path to the batch file in the program/script section. If you skipped step 2, enter the path to the python executable in the program/script section and enter the path to your python script file in the “Add arguments (optional)” section.
  • If your script has some dependencies installed in some folder, like browser executable, you can include the folder path in the “Start in (optional)” section and click next.

If you made a batch file

If you didn’t make a batch file

  • 8: Lastly, check all the entered values and click finish.

Now, at your chosen time, your script will execute.

Creating the exe should be the best method. But if you want to run it with the task scheduler you can do it in this way:

  1. Launch Window’s Task Scheduler
  2. Look for the The Actions pane(on the right) it has the Create Basic Task action. Click on it.
  3. This will open a wizard where you will define the name of your task, the trigger (when it runs), and the action (what program to run).
    Action tab is where you specify the name of your Python script to run as well as any arguments to the script.

To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of Python is used in mixed environments (64bit or 32bit), it is recommended that you run the Python executable with the name of your Python file as an argument to the executable.

Suppose the script you want to run is E:My script.py. Instead of running the script directly, instruct the task scheduler to run python.exe with the script as an argument. For example:

C:Python27ArcGIS10.2python.exe «E:My script.py»

The location of python.exe depends on your install. If you don’t know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location of python.exe as well as other information about your Python environment.

import sys
import platform
import imp

print("Python EXE     : " + sys.executable)
print("Architecture   : " + platform.architecture()[0])
print("Path to arcpy  : " + imp.find_module("arcpy")[1])

raw_input("nnPress ENTER to quit")

After determining the location of python.exe, this is what is entered in the Action panel of the task scheduler:
enter image description here

If there are additional arguments (parameters) to your script, provide them after the path to your script. Hope this helps.

В одной из записей блога я писал как можно автоматизировать выполнение скрипта при помощи cron и linux. Тогда речь шла о WSL (подсистема Linux для Windows). К сожалению с производительностью у WSL пока не все гладко, поэтому пришлось все портировать на Windows.

Сам вопрос запуска скрипта по расписанию в Windows на самом деле очень прост. Нужно лишь создать батник (файл с расширением .bat) прописать в нем все необходимые манипуляции и настроить выполнение данного файла в планировщике. Делается это так:

  • В текстовом редакторе создаете новый файл
  • Добавляете в него ваш код, в моем случае это одна строчка
    C:UsersUser.virtualenvssite-GQljvJBGScriptspython.exe «D:/dev/site/backend/cron.py»
  • Сохраняете файл с расширением .bat, например cron.bat.
  • В планировщике прописываете его выполнение

Особенности запуска Django кода

Если вы пропишите запуск какого нибудь Django скрипта то получите ошибку.

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

Данная ошибка говорит о том, что для запуска скрипта нужн о сконфигурировать окружение, погружаться в дебри конфигураций не буду, отмечу лишь вариант решения проблемы.
В начале своего файла добавьте строчки кода

import os
import django<br>os.environ["DJANGO_SETTINGS_MODULE"] = 'project.settings'
django.setup()

Где ‘project.settings’ путь к файлу settings.py вашего Джанго проекта.

In this article, we are going to see how to schedule a Python Script to run daily. Scheduling a Python Script to run daily basically means that your Python Script should be executed automatically daily at a time you specify.

Preparing the Python  Script.

Create a Python Script that you want to schedule. In our example, we made a simple Python Script that will Open our Mail account to check our emails daily. You’ll need to import the ‘webbrowser’ module.

Alternatively, you may use any Python script that you’d like to schedule.

Python3

import webbrowser

url = "mail.google.com"

chrome_path = r'C: Program Files(x86)Google/

ChromeApplicationchrome.exe'

webbrowser.register(

  'chrome', None, webbrowser.BackgroundBrowser(chrome_path))

webbrowser.get('chrome').open_new_tab(url)

Note: Change your chrome_path according to the location on your system if you want to use the above Python Script.

Now there are two ways to schedule a script:

  • Using batch files.
  • Using Windows Task Scheduler.

Method 1: Using batch file

Step 1: Create a Batch File.

Open Notepad and follow this generic structure :

“Path where your Python exe is storedpython.exe” “Path where your Python script is storedscript name.py”

pause

Example of the batch file:

Finally, save the Notepad with your file name and the “.bat” extension anywhere on your machine,  Eg –  ‘automation.bat’.

Method 2: Using Windows Task Scheduler.

Step 1: Open Task Scheduler Application on your Windows Machine.

Location -> C:ProgramDataMicrosoftWindowsStart MenuProgramsAdministrative ToolsTask Scheduler

Step 2: Click on ‘Create Basic Task….’ in the Actions Tab. And give a suitable Name and Description of your task that you want to Automate and click on Next.

Step 3: In the next step, you have to select at what time intervals your script should be executed. Select ‘Daily’ and click Next. Now you need to specify at what time your Python Script should be executed daily and then Click on Next.

Step 4: In the next step, you need to select the ‘Start a Program’ option from the given options and then click Next. And there choose (Optional – for users who created Batch File) Now if you followed ‘Batch file step’, you just need to enter the file location of the Batch file you created followed by ‘yourbatchfilename.bat’ in Program/Scripts field and click on Next and Finish your Task, your python Script will now run daily at your specified time. 
In this section, you’ll need the file location of the following files:

  • python.exe – The path where python.exe is stored (you also need to add ‘python.exe’ at the end of the location.)
  • the path where your python script file is stored.

In our case : 

Path of python.exe  –  C:Python39python.exe

Path of my python script  –  D:TutorialsPython

Step 5: Add respective file locations as shown in the figure below and arguments input, specify your python script name.

Step 6: In the next tab, you just need to verify your Inputs and then click on ‘Finish’.

That’s it, your Python Script is now Scheduled and will be executed daily at your Specified time. Here is the Screenshot of our Python Script that got executed.


  Перевод


  Ссылка на автора

Кредит: Stocksnap

Запускать мои скрипты Python каждый день слишком хлопотно.

Мне нужен способ периодически запускать мои скрипты Python

Представьте, что ваш менеджер просит вас проснуться среди ночи, чтобы запустить сценарий. Это будет ваш самый большой кошмар. Вы просыпаетесь преждевременно, подвергаетесь воздействию ужасного синего света и избегаете приличных снов каждую полночь.

Как любой специалист в области данных, вам может потребоваться запустить несколько сценариев для создания отчетов или развертывания аналитического конвейера. Следовательно, вам нужно узнать опланировщикичтобы не испортить выходные.

Каждый инженер данных и ученый в один момент времени должен выполнять периодические задачи.

По определению, периодические задачи — это задачи, которые выполняются многократно в течение определенного промежутка времени без вмешательства человека или с минимальным вмешательством. В период быстрого развития данных и технологий вам необходимо запускать сценарии для создания резервных копий баз данных, потоковой передачи в Twitter и т. Д.

К счастью, с помощью Task Scheduler вы теперь можете запускать свой скрипт Python для выполнения периодических задач каждый день / неделю / месяц / год в зависимости от ваших потребностей.

В этом уроке вы узнаете, как запустить планировщик задач для веб-данные из Lazada (электронная коммерция) и поместите его в СУБД SQLite База данных.

Это быстрый способ запустить ваш скрипт автоматически!

Запуск веб-скриптинга с помощью планировщика задач и добавление его на диск SQLite

Давайте начнем!

Методы

В этом руководстве мы будем использовать планировщик задач Windows для запуска сценария bat, который будет запускать сценарии Python. Для выполнения этих скриптов у нас есть два простых шага:

  1. Создать исполняемые файлы Python (bat-файл)
  2. Настроить задачу в планировщике задач Windows

Однако, если вы являетесь пользователем Linux и не имеете доступного планировщика задач Windows, вам следует использовать cron планировщики,

Создание исполняемого файла Windows для запуска Python

BAT файлэто DOSпакетный файлиспользуется для выполнения команд с помощью командной строки Windows (cmd.exe). Он содержит ряд команд строки, которые обычно могут вводиться в командной строке DOS.BAT файлычаще всего используются для запуска программ и запуска служебных программ в Windows. — fileinfo.com

Используя bat-файл в качестве нашего исполняемого файла, мы сохраним наш скрипт run в файле, а затем дважды щелкните файл bat, чтобы выполнить команду cmd (командная строка) для запуска скрипта python.

Все, что вам нужно сделать, это создать новый файл bat (например, web-scraping.bat) и написать исполняемый скрипт в формате, Вы можете добавитьПаузакоманда, чтобы избежать закрытия командной строки после выполнения.

C:new_softwarefinanceScriptspython.exe "C:/new_software/Web Scraping/Web-Scraping/Selenium Web Scraping/scraping-lazada.py"
pause

После того, как вы дважды щелкнете по этому файлу bat, Windows откроет вашу командную строку и запустит инструмент веб-поиска. Чтобы запланировать этот двойной щелчок / выполнение, мы подключим наш планировщик задач к файлу bat.

Настроить задачу в планировщике задач Windows

Планировщик задач Windowsявляется приложением Windows по умолчанию для управления задачами в ответ на триггер на основе событий или времени. Например, вы могли бы предложить определенный щелчок и компьютерные действия (такие как перезагрузка) или даже предложить время, каккаждый первый день финансового кварталавыполнить задачу.

В более широком плане эта задача будет содержать сценарий и метаданные, чтобы определить, что и как будет выполняться действие. Вы можете добавить определенный контекст безопасности в аргумент и контролировать, где планировщик будет запускать программу. Windows будет сериализовать все эти задачи как.JOBфайлы в специальной папке под названиемПапка задач,

Поток процессов Task Scheduler для автоматизации работы веб-приложений

В этом руководстве мы собираемся установить событие, основанное на времени, для запуска нашего приложения и вывода данных в SQLite. Всего там

  1. Нажмите Пуск Windows, найдите планировщик задач и откройте его.
  2. Нажмите Create Basic Task в правом окне.
  3. Выберите время запуска.
  4. Укажите точное время для нашего предыдущего выбора.
  5. Запустить программу
  6. Вставьте скрипт вашей программы, где вы сохранили свой bat файл
  7. Нажмите Готово.

Давайте начнем!

  1. Нажмите Пуск Windows, найдите планировщик задач и откройте его.,

Планировщик заданий Windows

2.Нажмите Create Basic Task в правом окне.,

Вы должны указывать имя задачи (например, веб-очистка) и описание (например, веб-очистка и дамп SQLite автоматически каждый день в 18:00)

3.Выберите время срабатывания,

У вас будет возможность выбрать временной триггер ежедневно, еженедельно и даже ежемесячно. Логически этот выбор во многом зависит от того, как часто вы хотите обновить значения из вашего источника данных. Например, если ваша задача — очистить баланс MarketWatch Stocks, вы должны запускать сценарии каждый финансовый квартал.

4.Выберите точное время для нашего предыдущего выбора,

Мы выберем месяц январь, апрель, июль и сентябрь, чтобы указать весь ранний финансовый квартал.

5 Запустить программу

Здесь вы сможете запускать скрипты Python, отправлять электронную почту и даже отображать сообщение. Не стесняйтесь выбирать те, которые вам наиболее удобны. Однако вам следует остерегаться устаревших задач, которые будут удалены в последующих исправлениях.

6.Вставьте скрипт вашей программы, где вы сохранили свой bat файл

Это запустит планировщик задач для вашего скрипта Python для автоматизации. Убедитесь, что вы также включили Пуск в папку вашего приложения, чтобы получить доступ ко всем соответствующим элементам (исполняемые файлы Selenium Browser / диск SQLite)

7.Нажмите Готово,

Вы можете проверить созданное расписание задач на первой странице Планировщика задач.

Поздравляем, вы установили свой первый автоматический планировщик в Windows.

Результат

Вот анимация GIF для ваших ссылок. Обратите внимание, как планировщик сам запускает скрипты Python. Как только сценарии завершатся, он извлечет извлеченное значение из базы данных SQLite. В будущем это приложение будет запускаться каждый раз, когда выполняется условие триггера, и добавлять обновленные значения в SQLite.

Запуск веб-скриптинга с помощью планировщика задач и добавление его на диск SQLite
Данные добавляются в SQLite с помощью планировщика задач

В заключение…

Мальчик смеется, читая книгу, источник: Unsplash

Я действительно надеюсь, что это было отличное чтение и источник вдохновения для вас, чтобы развиваться и вводить новшества.

пожалуйстаКомментарийниже, чтобы предложить и отзывы.

Если вам действительно это нравится, пожалуйста, проверьте мой профиль. Есть больше о статьях Data Analytics и Python Projects, которые будут соответствовать вашим интересам.

Удачного кодирования :)

об авторе

Винсент Татан — энтузиаст данных и технологий, имеющий соответствующий опыт работы в Visa Inc. и Lazada для реализации микросервисных архитектур, бизнес-аналитики и аналитических конвейерных проектов. ,

Винсент — коренной индонезийец, имеющий достижения в решении проблем, а также сильные стороны в разработке полного стека, анализе данных и стратегическом планировании.

Он активно консультирует SMU BI & Analytics Club, руководит начинающими учеными и инженерами в области данных из разных областей и раскрывает свой опыт для бизнеса в разработке своих продуктов.

Пожалуйста, свяжитесь с Винсентом через LinkedIn , средний или YouTube канал

In this post, you’ll see the steps to schedule a Python script using the Windows Scheduler.

Step-1: Prepare the Python Script

For example, let’s suppose that the goal is to display ‘Hello World!’ each day at 6am.

Here is the Python script to be used for our example (you may use another Python script based on your needs):

import tkinter as tk 

root= tk.Tk() 
 
canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

label1 = tk.Label(root, text='Hello World!')
canvas1.create_window(150, 150, window=label1)

root.mainloop()

Step-2: Save the Python Script

Once you’re done writing the script, save it as a Python file (where the file extension is .py):

For instance, let’s save the file as hello_world.py under the following path:

C:UsersRonDesktophello_world.py

Step-3: Create Batch File to Run the Python Script

Next, create a batch file to run the Python script.

To start, open Notepad, and then use the following template:

"Path where your Python exe is storedpython.exe" "Path where your Python script is storedscript name.py"
pause

For our example:

  • The path where the Python exe is stored is:
    “C:UsersRonAppDataLocalProgramsPythonPython39python.exe”
  • The Path where the Python script is stored is (see step-2):
    “C:UsersRonDesktophello_world.py”

Here are the paths in the Notepad (you’ll need to adjust those paths to fit your instances):

"C:UsersRonAppDataLocalProgramsPythonPython39python.exe" "C:UsersRonDesktophello_world.py"
pause

Finally, save the Notepad with your file name and the “bat” file extension:

file_name.bat

For example, let’s save the Notepad as:

run_python_script.bat

Once you save the Notepad, a new batch file (called run_python_script) will be created at the specified location:

C:UsersRonDesktoprun_python_script.bat

Step-4: Schedule the Python Script using the Windows Scheduler

In order to schedule the Python script using the Windows Scheduler:

  • Open the Windows Control Panel and then click on the Administrative Tools
  • Double-click on the Task Scheduler, and then choose the option to ‘Create Basic Task…’
  • Type a name for your task (you can also type a description if needed), and then press Next. For instance, let’s name the task as: Run Hello World
  • Choose to start the task ‘Daily‘ since we wish to run the Python script daily at 6am. Also specify the start date and time (6am for our example)
  • Select, Start a program, and then press Next
  • Use the Browse button to find the batch file (run_python_script.bat) that runs the Python script. In our case:

C:UsersRonDesktoprun_python_script.bat

Finally, click on Finish, and you should be good to go. From this point onward, you’ll be greeted with ‘Hello World!’ everyday at 6am.

Sometimes we want our script to run at a specific time or a specific number of times. We can automate this task so we don’t need to run the script manually. Windows provides a software allied task scheduler that will run our script at a given time.

Limitations:

  1. Works only on Microsoft Windows operating system
  2. Your Computer needs to be switched on at the scheduled time
  3. Doesn’t not imitate Server like – specific time execution. So in order to run a code at any time without the hassle of doing it self (switching the computer on) – you need to do using a Server (execution of script on server)

Steps to use the Windows Task Scheduler

Let’s get right into the steps to schedule the execution of Python scripts right away. Follow through and let us know if you have any questions later! We’ll make use of the Windows task scheduler for easier setup.

1. Open the task scheduler

Open the task scheduler application on your computer by searching for Task Scheduler in the start menu.

Task Scheduler
Task Scheduler

2. Create a new task

Next, create a task in the task scheduler by right-clicking on the Task Scheduler (Local).

Create A New Task In Scheduler
Create A New Task In Scheduler

3. Name the task

Add a name so you can identify it later.

Name The Task
Name The Task

4. Create a new action for our script

Switch to the “Actions” tab and add a new action. This is where the real stuff happens.

Create New Action
Create New Action

5. Run python script

To run the python script automatically using Windows scheduler we need to follow the below steps in our code:

  • We need the path of our python installation which we can find by running the following python code.
import sys
print(sys.executable)
Python Installation Path
Python Installation Path
  • We’ll paste this location in box number 1 of our Windows Scheduler action
  • In the box number 2 we’ll pass the name of the script file (python file)
    Ex: python_sample_file.py or a.py
  • In box number 3 we’ll pass the path of our Python executable (python file)
Entries In Action Of Windows Task Scheduler
Entries In Action Of Windows Task Scheduler

6. Create a trigger for the Script

A trigger means an event that causes our script to run. For example, we can specify a time on which our script will get executed. It provides options like what day, what time, how many times, for how long our script should get executed.

Hence, you’ll need to enter all the required details of it.

Trigger In Scheduler
Trigger In Scheduler

For our program, we’ll set a specific time that will trigger our following script to execute:

print("Hi, this is scheduled message")

On successful creation of our Windows Task Scheduler and upon the trigger of the python script file, the script is executed and the output is displayed as follows in the Windows Command Prompt:

Output Of Task Scheduler
The output of Task Scheduler

Conclusion

Hope you have learned well how to run your python script file at any desired time. Scheduling a task, which is quite useful in attaining automation of your script file.

  • October 21, 2021
  • Author —
    Chetan Ambi
  • Category —
    Python

Automate Python scripts using Schedule

Introduction

You have written lot of Python scripts for the daily tasks such as sending mails, processing excel/csv files, currency conversion, web scraping, stock prices tracking, etc. and but now you want to automate/schedule them. One good approach is to schedule these scripts as per your need; daily at 5 PM or weekly once on Sunday 12 AM etc. 

There are a number tools available at your disposal such as — schedule, apscheduler, python-crontab, apache-airflow, etc. that you can use to schedule your Python jobs . In this blog post, we will use the schedule library for scheduling the Python scrips.

Schedule

The schedule package is a simple and an elegant solution that can help you automate daily tasks such as sending mails, processing excel/csv,  etc. 

As mentioned in the official documentation, schedule package is not what you need if you are looking for:

  • Job persistence (remember schedule between restarts)
  • Exact timing (sub-second precision execution)
  • Concurrent execution (multiple threads)
  • Localization (time zones, workdays, or holidays)

If your requirement is to automate/schedule scripts in production, then I would recommend you explore Apache-AirFlow and Apscheduler. These provide a lot more functionalities than the schedule package. We will also cover these in future blog posts. Stay tuned!

Also note that the author of the library, Dan Bader, says, “schedule is a job scheduler well-suited for periodic tasks, preferably ones with relatively small intervals between executions”.

Installation

Examples

Let’s try to understand how to use the schedule library for scheduling Python scripts with a simple example below. The script schedules and executes the function named job every 5seconds starting from the moment you ran the code. You can see that the code is self-explanatory. 

				
					# myscript1.py
import schedule
import time

def job():
    print("I'm working...")
schedule.every(5).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
				
			
				
					(base) C:UsersswathDesktop>python myscript1.py
I'm working…
I'm working…
I'm working…
				
			

That is just one simple example. But you can schedule the jobs based on your requirement. The below examples give you some idea about different ways of scheduling your Python jobs. 

				
					# Run the job to run every 5 minutes from now
schedule.every(5).minutes.do(job)

# Run the job to run every 5 hours from now
schedule.every(5).hours.do(job)

# Run the job to run every 5 days from now
schedule.every(5).days.do(job)

# Run the job to run every 5 weeks from now
schedule.every(5).weeks.do(job)

# Run job on a specific day & time of the week
schedule.every().monday.do(job)
schedule.every().sunday.at("06:00").do(job)

# Run job every day at specific HH:MM and next HH:MM:SS
schedule.every().day.at("10:30:00").do(job)
				
			

Using a decorator to schedule the job

Alternatively, you can also use @repeat decorator to schedule the jobs. To schedule the job at every 5 mins, you could use the below code . There is little difference in syntax but it works similarly as we saw in the previous examples. 

				
					# myscript2.py
from schedule import every, repeat, run_pending
import time

@repeat(every(5).minutes)
def job():
    print("Job scheduled using decorator...")
    
while True:
    run_pending()
    time.sleep(1)
				
			
				
					(base) C:UsersswathDesktop>python myscript2.py
Job schedule using decorator...
Job schedule using decorator...
...
				
			

Passing arguments to the job

Sometimes you may want to pass the argument to the job. Since it’s a function we are scheduling, you can pass positional and keyword arguments as per your need. Refer to the example below — 

				
					# myscript3.py
import time
import schedule
import pandas as pd

def job(type):
    if type == 'xlsx':
        df = pd.read_excel('report.xlsx')
    elif type == 'csv':
        df = pd.read_csv('report.csv')
    print(f'{type} file is read successfully')

schedule.every(1).seconds.do(job, type='xlsx')
schedule.every(2).seconds.do(job, type='csv')

while True:
    schedule.run_pending()
    time.sleep(1)
				
			
				
					(base) C:UsersswathDesktop>python myscript3.py
xlsx file is read successfully
csv file is read successfully
xlsx file is read successfully
xlsx file is read successfully
....
				
			

Retrieve all the jobs

If you want to see all the jobs that are scheduled then you can use get_jobs() method. 

				
					# myscript4.py
import schedule

def job():
    print('Hello world')
    
schedule.every().second.do(job)
schedule.every().minute.do(job)
schedule.every().hour.do(job)

print(schedule.get_jobs())
				
			
				
					(base) C:UsersswathDesktop>python myscript4.py
[Every 1 second do job() (last run: [never], next run: 2021-10-09 15:36:50), 
Every 1 minute do job() (last run: [never], next run: 2021-10-09 15:37:49), 
Every 1 hour do job() (last run: [never], next run: 2021-10-09 16:36:49)]
				
			

Cancelling the job(s)

You can cancel one or more jobs using cancel_job(<job_name>) and clear() methods respectively. 

				
					# myscript5.py
import schedule

def greet():
    print('Welcome to Python Simplified !!')

job1 = schedule.every().second.do(greet)
job2 = schedule.every().minute.do(greet)
job3 = schedule.every().hour.do(greet)

print('# of jobs scheduled', len(schedule.get_jobs()))

# Cancelling job1
schedule.cancel_job(job1)
print('# of jobs scheduled', len(schedule.get_jobs()))

# Cancelling all jobs (job2 and job3)
schedule.clear()
print('# of jobs scheduled', len(schedule.get_jobs()))
				
			
				
					(base) C:UsersswathDesktop>python myscript5.py
# of jobs scheduled 3
# of jobs scheduled 2
# of jobs scheduled 0
				
			

Running jobs until a certain time

If you want to schedule the jobs to run until a certain time, you could use until method. The following examples help you get started using until method.

				
					# myscript6.py
import schedule
from datetime import datetime, timedelta, time

def job():
    print('Python Simplified')

# Run job until a 15:30 today
schedule.every(1).hours.until("15:30").do(job)

# Run job until a 2030-01-01 18:33 today
schedule.every(2).hours.until("2021-10-31 23:59").do(job)

# Run job to run for the next 12 hours
schedule.every(3).hours.until(timedelta(hours=12)).do(job)

# Run my_job until today 23:59:59
schedule.every(4).hours.until(time(23, 59, 59)).do(job)

# Run job until a specific datetime
schedule.every(5).hours.until(datetime(2021, 12, 31, 23, 59, 59)).do(job)
print(schedule.get_jobs())
				
			
				
					(base) C:UsersswathDesktop>python myscript.py
[Every 1 hour do job() (last run: [never], next run: 2021-10-10 10:37:28), 
Every 2 hours do job() (last run: [never], next run: 2021-10-10 11:37:28), 
Every 3 hours do job() (last run: [never], next run: 2021-10-10 12:37:28), 
Every 4 hours do job() (last run: [never], next run: 2021-10-10 13:37:28), 
Every 5 hours do job() (last run: [never], next run: 2021-10-10 14:37:28)]
				
			

How to run the thread in the background

If you have noticed all the code examples above and those from the official documentation, you realize that the jobs you are scheduling are blocking the main thread. So, how to continuously run the scheduler in the background without blocking the main thread?

You can achieve this by creating your own thread and let it run in the background without blocking the main thread. Refer to the below example. It’s inspired from the official example given here.

In the below example, 

  • run_continuously() function creates a thread and returns threading event cease_continuous_run.
  • background_job_1 and background_job_2 are the two jobs that you want to run in the background without blocking the main thread.
  • In the main code, you are scheduling both the jobs to run every second.
  • Next, when run_continuously() function is called, it starts the thread for both the background jobs. The jobs will run in the background even when the main function is completed. Refer to the output shown below and you will understand. 
  • If you want you can also stop all the background jobs by running stop_run_continuously.set().

I suggest you go play around with the below code to better understand how you can schedule the jobs to run in the background without blocking the main thread.

				
					import schedule
import logging
import threading

def run_continuously():
    cease_continuous_run = threading.Event()
    class ScheduleThread(threading.Thread):
        @classmethod
        def run(cls):
            while not cease_continuous_run.is_set():
                schedule.run_pending()
    continuous_thread = ScheduleThread()
    continuous_thread.start()
    return cease_continuous_run

def background_job_1():
    logging.info("Background 1 thread running...")

def background_job_2():
    logging.info("Background 2 thread running...")

if __name__ == "__main__":
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
    logging.info("Main - starting thread")
    schedule.every().second.do(background_job_1)
    schedule.every().second.do(background_job_2)
    stop_run_continuously = run_continuously()
    logging.info("Main: completed !!")
    time.sleep(5)
    stop_run_continuously.set()
				
			

Output: If you comment last 2 lines in the above code, the background jobs will run continuously

				
					PS C:UsersswathPycharmProjectspythonProject> python main.py
20:43:37: Main - starting thread
20:43:37: Main: completed !!
20:43:38: Background 1 thread running…
20:43:38: Background 2 thread running…
20:43:39: Background 1 thread running…
20:43:39: Background 2 thread running…
20:43:40: Background 1 thread running…
20:43:40: Background 2 thread running…
20:43:41: Background 1 thread running…
20:43:41: Background 2 thread running…
				
			

Disadvantages

The main disadvantage with schedule package is that you need to keep your computer up and running all the time in order to run the scripts in the background. This should not be a problem for non-production tasks but for production tasks you may have to create your own server or use some 3rd party cloud services.

As mentioned earlier, schedule is not suited for the tasks that require job persistence, concurrent execution, localization, etc.

References

Chetan Ambi

Chetan Ambi

A Software Engineer & Team Lead with over 10+ years of IT experience, a Technical Blogger with a passion for cutting edge technology. Currently working in the field of Python, Machine Learning & Data Science. Chetan Ambi holds a Bachelor of Engineering Degree in Computer Science.

Chetan Ambi

Chetan Ambi

A Software Engineer & Team Lead with over 10+ years of IT experience, a Technical Blogger with a passion for cutting edge technology. Currently working in the field of Python, Machine Learning & Data Science. Chetan Ambi holds a Bachelor of Engineering Degree in Computer Science.

A Two-Part Post that Shows You How to Automate Your Python Script Execution

PART
1: Create a Batch File to Execute a Python Script

One of the most important things I do on my virtual machine is automate batch jobs. Let’s say I want to send Susie an automated email reported generated via a Python script at 6 am every day—I want to automate the task of generating and sending that email via Python using a batch job on my virtual machine (VM). This tutorial walks you through automating the process and setting up your computer (or VM) to run Python jobs on a schedule.

First, we select a Python script that we want to automatically execute. The one I’ve picked below is named python_example_script.py:

"""
This is the sample Python file that we want to automate in batch mode
"""
import pandas as pd

def main():
    #Create a dummy pandas dataframe 
    dummy_df=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
    #Write the dataframe to a csv
    dummy_df.to_csv('sample_dummy_file.csv')
    print('Automating the boring work makes our lives so much better!')
    
if __name__== "__main__":
  main()

Above is a very simple Python script, where we generate a pandas dataframe and save it to a csv, called sample_dummy_file.csv. We then output a print statement.

Next, we create a .bat file that will automatically execute the Python script from the Windows command line.

We want to call the Python script directly
from the Command Prompt and execute it. I have saved the Python script that I
want to execute under my Documents/Blog/BatchMode directory, so I locate the
directory path:

Locate the Python script in the File Folder that we want to automate

Highlighted is the Python file that we want
to automate, python_example_script.py, as well as the Directory Path.

I open a fresh Notepad document and type the following:

cd "C:UsersDocumentsBlogBatchMode"

python python_example_script.py

Let’s walk through what the above script means:

The ‘cd’ command at the beginning references the change directory. It is the OS command to change the current working directory.

The directory that we want to point to in question is referenced immediately after cd—”C:UsersDocumentsBlogBatchMode”. We need to point to this directory for the command prompt to find the python_example_script.py file.

To get the EXACT directory path that we want to reference, we right click on the ‘BatchMode’ file path in the file folder, and press ‘Copy address as text’. This will save the exact directory path that we want to use, and we can paste it right after the cd command.

Get the file directory name that your Python file is sitting in

On the second line of the file is the ‘python’ statement. This is telling the Command Prompt that we want the Python.exe application to perform an action; in this case, execute the python_example_script.py, which is right after the ‘python’ command.

IMPORTANT: For the Command Prompt to recognize ‘python’ as an application, the python application needs to be added to the Windows PATH. When you first installed Python, you had the option to add Python to the Windows Path. If you enabled this option—great, you should be good to go! If not, you’ll need to add Python to the Windows Path. This tutorial walks you through how to do this.

Finally, we want to save the batch file
that we have just created in Python. When we save the file, we want to save it
as a .bat file, as shown below:

Be sure to save your file as a .bat extension!

Be sure to use ‘All Files’ under the ‘Save as type’ option, or you won’t be able to save the file as a .bat extension!

Finally, we execute the batch file to ensure that it works properly.

We find the .bat file in the in the BatchMode directory that we just created, and we simply press on it. The Command Prompt should automatically open, and the script should start executing, as shown below:

Executing the Python batch file: the python file will execute via the command line when the batch file is manually pressed

As you can see, the Command Prompt opens in the “C:UsersDocumentsBlogBatchMode” directory, and we execute the python_example_script.py file!

This concludes Part 1 of this tutorial. If you want to learn how to schedule this Python file to run automatically using Windows Task Scheduler, read on to Part 2 of this tutorial.

As always, the code for this tutorial is available for your viewing pleasure via my GitHub account, under the following URL:

https://github.com/kperry2215/batch_mode_script_automation

Thanks for reading!

Like this post? Please share to your friends:
  • Как запускать скрипт по расписанию windows
  • Как запустить boot menu на windows 10
  • Как запускать скрипт python при запуске windows
  • Как запускать рабочий стол сразу в windows 8
  • Как запускать программы на python windows