Easily Automate Your Python Scripts!

Shashank Vemuri
Analytics Vidhya
Published in
8 min readJun 8, 2020

Ever wish your Python Scripts could just automatically run?

The power of automation nowadays is undeniable. Nobody is willing to do any more than they need to and people are always looking for shortcuts in every aspect of life. Of course, people may think this is just lazy but I believe the beauty of creativity is finding an easier way to do things.

Photo by Franck V. on Unsplash

I used to have to open up my Spyder IDE and run each one of my finance algorithms every day but I knew there had to be an easier way. I did some research and read countless Stack Overflow articles and fortunately found out there was!

Before I go into the process, I just want to touch upon the importance of this automation, for coding in general, and especially with Python for financial use. The market is changing at a rapid pace and financial data can become outdated within weeks if not days.

Many coding algorithms are run on a regular basis whether it be day-to-day or month-to-month or even year-to-year and to be able to automate this process is invaluable. The amount of wasted time running these scripts adds up over time and can be extremely boring/tiring.

Now continue reading to find out how to perform this automation on Windows and macOS!

When starting this process, the first step is to turn these Python scripts into applications that you can launch with just a click of a button. Second, you have to schedule these applications to run so they start without you having to think twice.

Windows

Part 1: Turn The Python Script Into an Application¹

In order to accomplish Part 1 for the Windows users, you have to create a Batch file that is going to navigate through the directories, select the preferred script, and launch it. To achieve this, you need to open any text editor and write the following lines:

REM This is a comment cd 'path_to_file'
file_name.py neonphotography
ECHO Ran first batch PAUSE

Replace ‘path_to_file’ with the directory which contains the file (example: coding/Finance) and file_name.py with the actual file name. Note: Do not actually put the file path in quotes.

The Batch file will launch the console from the folder the .bat file will be saved in. If you put your .bat file in the Documents folder, you will not be able to reach scripts that are located in folders that are located one folder up unless you plan for that inside of your bat file (by using the cd command).

Now you must save the file with the .bat extension. This will create an executable file that will run the tasks written inside it when clicked on. You can now double click on your file and watch the console fetch your script and launch it.

Part 2: Schedule the Python Script Application To Run²

Now that we have our .bat file, the simplest way to schedule their execution is to use the already installed Windows Task Scheduler in Windows.

Photo courtesy of Yaniss Illoul with martechwithme!

In order to not accidentally mix ourselves up with the important tasks that our computer is performing, it is better to create a new folder that will contain all of our scheduled Python scripts.

Create a “New Folder” under the folder “Task Scheduler Library” on the left side of the window.

Photo courtesy of Yaniss Illoul with martechwithme!
Photo courtesy of Yaniss Illoul with martechwithme!

We can create tasks that will be easy to both get to and change inside this new folder. In order to create a simple schedule for a new task, click on “Create a Baci Task” and follow the instructions given. For the rest of the article, however, we are going to explore the advanced method that allows for a larger amount of customization.

After having clicked on “Create Task”, name your task something simple and easy-to-remember. You can change the rest of the settings to your preference.

Photo courtesy of Yaniss Illoul with martechwithme!

In the “Triggers” tab at the top, there will be an option to actually set the schedule for when you want the Python script to run as well many other useful parameters.

Photo courtesy of Yaniss Illoul with martechwithme!

Once you are done with this section, the “Actions” tab will contain the specifics on what application will be running and what the task will actually do. You can click on the “New” button and this will open a “New Action” window.

Photo courtesy of Yaniss Illoul with martechwithme!

You can now select the .bat file from earlier that launches the Python script under the “Browse” option.

You must also fill in the full path to your batch script location in the “Start in” section. This part is not optional and will not start the task. An example would be C:\Users\Shashank\Desktop if my .bat file was located under C:\Users\Shashank\Desktop.

Photo courtesy of Yaniss Illoul with martechwithme!

You can now save the task and it will now appear in the list of tasks in the “Automation” folder. It’s finally done!

macOS

Part 1: Turn The Python Script Into a Unix Executable¹

In order to accomplish Part 1 for the macOS users, you have to first create an executable file that will automatically open up the specified Python script.

To start this process, open up any text editor and input these lines:

#!/bin/sh
Python /Users/Shashank/Documents/Code/Finance/file_name.py neonphotography

Of course, you must the file path to your desired file path with the python file at the end. Remember to include the ‘#!/bin/sh’ as the first line, the Python before the file path and the ‘neonphotography’ after the file path. Do not save the file with any extension.

Copy the file path of this new file and launch the Terminal.

Type out “chmod 755” and then paste the path name to the file and then press enter. Refer to the image below for further clarification.

Example

This command will convert the new file into a Unix executable that can launch the script simply with a double click.

Now in order to schedule the Unix executable, you must turn it into an application first. Luckily, you can just launch the already-installed Automator application on macOS and follow the guide below on how to do just that.

Once Automator is open, select to create an “Application”.

Choose “Application” on the Automator Application

There will be many actions that you can drag and drop into the right hand of the screen. For the purposes of this tutorial, you will be using two actions: “Get Specified Finder Items” and “Open Finder Items.”

Select the “Get Specified Finder Items” action on the left side of the page. Once it is added on the right panel in Automator, there will be an option to “Add” an item to this action. Press this button, browse through your files, and select your previously created Unix Executable.

Then, select the “Open Finder Items” and drag it below the “Get Specified Finder Items” as shown in the image below. This will actually open the file that is specified in the action above.

Example

Lastly, save the file as an Application with an easy-to-remember name!

Save as Application in ‘File Format’

Part 2: Schedule the Python Script Application To Run²

Now in order to actually schedule the Python script to run, you must open your Apple Calendar app (not Google or any other type of calendar application). Select your iCloud Calendar account and then create a new event.

The Calendar Event

Under the alert section as seen as the last option below, choose the “Custom” option and then choose “Open file” under the options.

Choose the appropriate .app file you would wish to launch once the event has started and set the “Alert” to start “At time of event.”

In order to change how often the script runs, you can change the “Repeat” setting of the event to your preference.

That’s it! This process will allow you to run your scripts for however long and as often as you would like. Unfortunately, the machine must be turned on for the calendar event to actually run but in case your computer is off at the time of the event, the event will start once you turn it on again.

Thank you so much for your time! I hope this article will help you in your coding journey and hopefully, it saves you an abundance of time down the road.

References

[1]: “Convert Python Scripts To Executable Applications For Mac And Windows”. Martechwithme.Com, 2020, https://martechwithme.com/convert-python-script-app-windows-mac/.

[2]: “How To Schedule Your Python Scripts On Mac And Windows”. Martechwithme.Com, 2020, https://martechwithme.com/schedule-python-scripts-windows-mac/.

--

--

Shashank Vemuri
Analytics Vidhya

Hey! I’m Shashank Vemuri, a software engineer, stock trader, and entrepreneur.