How to Easily Automate Your Python Scripts on Mac and Windows | by Frank Andrade | Towards Data Science

Running a Python script could be as easy as opening your IDE or text editor and clicking the run button; however, if the script has to be executed daily or weekly, you don’t want to waste time repeating these steps over and over again.
Instead, you could automate those scripts by scheduling jobs that run your Python scripts at specific times. In this article, I’ll show you how to do this on Mac and Windows using the crontab and the task scheduler.
The Python script we’re going to automate it’s calledexample.py. You can use any script you have to follow along.
To schedule a job on macOS, we need first to make our Python file executable (.exe). There are different ways to convert .py files to executable files. In this case, I will use the “PyInstaller” library because of its simplicity.
PyInstaller reads a Python script and finds the libraries the script needs to be executed. Then it creates copies of all the files and puts them in a single executable file.
To install PyInstaller and make your python script executable do the following.
Open the terminal and run pip install pyinstaller. This will install PyInstaller
Inside the terminal, go to the directory where your script is located (use cd to navigate in the directories)
Once you‘re in the path where your script is located, write the followingpyinstaller --onefile example.py in the terminal to make the script executable.
After this, you’ll see a message that says “completed successfully.” Then in the directory where your script is located, a folder named “dist” should have been created. Inside the folder, you’ll find the standalone executable! Right-click on it, click “open with”, and then “terminal” to test whether everything works fine.
Note: If you see an error after running the standalone executable like this “ModuleFoundNotError: Not module named ‘ name_of_module’” you’ll have to repeat step 1 again, but now write the following
pyinstaller --onefile example.py --hidden-import name_of_module
To schedule jobs on macOS, we’re going to use crontab. To do so, open the terminal and run crontab -e. After this, your terminal should look like the picture below.
Here you can create a new job. Each of them has its own line. To create a job, use the format in the box below.
* * * * * command_to_execute* - minute (0-59)* - hour (0-23)* - day of the month (1-31)* - month (1-12)* - day of the week (0-6, Sunday to Saturday)
Learning how to create a new cron job is simple, but be careful cron job failures can be disastrous. I recommend you use crontab.guru to generate cron expressions in the correct format.
Now it’s time to create a cron job.
Hit i to activate theINSERT mode
Now you can write a job. In this example, I wrote 0 15 * * 5 path_of_executable This means “run the Python script every Friday at 15:00 ” (you can check this on crontab.guru)
After typing out, press esc . Then type : and write wqto save and exit (w - write, q - quit) and finally, press enter.
After this, the following window might show up
Just click “ok” to give access. After this, the crontab should have been created.
To verify the crontab was created, writecrontab -e or crontab -l. In case you want to remove all crontabs listed, write crontab -r.
That’s it! Now your Python script will run at the scheduled time!
Note: If you have problems with permissions, you need to give full disk access, so go to System Preferences, Security and Privacy, Privacy, and Full Disk Access.
Scheduling Jobs on Windows is easier than on Mac thanks to the task scheduler. Just follow these steps below. The Python script we’re going to automate it’s calledexample.py
A Batch file is used for different purposes, but in this case, we’ll use it as an executable file to automate our Python scripts.
We will store our Python script in a bat file, then click on the bat file to execute the command and run the Python script. To do so, follow these steps.
Right-click inside any folder and click “new” and create a notepad (“text document”)
Inside the notepad, write a text using the following format.
<Paste your python.exe location> <Paste your python script location>
To get the “python.exe” path (Python application path) you need to go to where Python is saved (check where you installed it). Your notepad might look like the text below.
"C:\User\Anaconda\python.exe" "C:\Scripts\example.py"
Now click “File” and then “Save as ...” and write any name including the extension .bat in the end. For this example, I’ll create a file named example.bat
Let’s schedule the example.bat file with Windows Task Scheduler. Just follow these steps.
Press the Windows button and search for Windows Task Scheduler. Then click on it.
Now you should see a window that looks like the picture above. Click “Create Basic Task” on the right panel.
A window like this should pop up.
Now you only have to fill in all the information needed and click next. These are the options you see above:
Create a Basic Task: Write your task name and description.Trigger time: Choose when you want the task to start. You can choose daily, weekly, monthly, and more. Then you have to pick the time for the previous selection (recur every x days, weeks, months)Action: There are 3 actions available but 2 of them are deprecated, so just choose “Start a program” and click next. Then, browse the bat file we created earlier.Click the “Finish” button to create the task.
Create a Basic Task: Write your task name and description.
Trigger time: Choose when you want the task to start. You can choose daily, weekly, monthly, and more. Then you have to pick the time for the previous selection (recur every x days, weeks, months)
Action: There are 3 actions available but 2 of them are deprecated, so just choose “Start a program” and click next. Then, browse the bat file we created earlier.
Click the “Finish” button to create the task.
After this, a new task should be listed on the Task Scheduler.
That’s it! Now you’re ready to automate any Python script on your computer.
Join my email list with 3k+ people to get my Python for Data Science Cheat Sheet I use in all my tutorials (Free PDF)