|

Create Batch Dot Bat File to Run Your Python Script With Windows Scheduler

After writing your script, you might want to schedule it to run periodically, let’s say, Daily, Weekly or Any Time interval you will decide

Before you schedule that you want to  simply create a batch file and schedule it run with a Windows Scheduler.

Here are simple steps to do that.

  1. First of all, if you want your script to run with a particular Python version you can decide that by declaring shebang line. For instance , if you want your script to run with Python 2.7 , you shebang line will be #! python2.7 shebang line
  2. This will ensure that your script runs without breaking due to any different Python version
  3. To create a batch file, simply open a new Notepad and
  4. Type the following echo C:\Users\adaba\Anaconda3\envs\gl-env\python.exe C:\TREES\customer_segmentation_maoting\buyer_behaviour_analysis.py %* PAUSE
  5. The echo will print out what is being executed, in this case our python script file being called and the results
  6. The first file path is the file path to the Python executable which want to use to run the Python script . (In my case , it is Python 2.7 as I have different versions installed)
  7. The second file path is the path to the Python script we want to run which in this case is buyer_behaviour_analysis.py
  8. The PAUSE will hold the command prompt open even after the file finishes executing and you can press any key to exit the command prompt window afterwards
  9. The echo and PAUSE are optional. You can ignore the PAUSE and the command prompt will close automatically once the script finishes running.
  10. Now save the file with .bat extension. eg runscript.batbatch file for python notepad
  11. You can test the batch file to see if it works by double click on it.
  12. Now that it works fine. You can schedule Window’s task for it to run periodically
  13. To do this , go to windows Scheduler by typing scheduler in the start menu search boxwindows scheduler
  14. Create a basic task windows scheduler create basic task
  15. Give it a name and descriptionscheduler name and description
  16. Select the PERIOD you want it run, whether, DAILY, WEEKLY, ONE TIME, etc
  17. On the NEXT TAB Set the DATE and TIME
  18. On the NEXT TAB , what action you want to do, choose Start a Program scheduler action you want to do
  19. On the NEXT TAB , BROWSE to the BATCH FILE you created; runscript.bat  and select that.
  20. Make sure to tick the “Open the Properties dialog for this task when I click Finish” on the next tab and click Finish. (The open the properties dialog will allow you to do some further configurations. )open the property dialogue windows scheduler
  21. One configuration you would want to choose is to click on the CHOOSE USER or GROUP button in addition to the other ticks as shown in the image belowchoose user or group windows scheduler
  22. In the popup window from the CHOOSE USER or GROUP, type the user as SYSTEM. This will allow the script to be run by system and it will not output any code or results to the user’s console. It will basically run the whole script in the background till completion. (It will therefore be ideal to have a logging system in your script to track some events. There is a simple note on python logging here)user system windows scheduler
  23. You can continue and do some other settings as you would need by using the tabs above the scheduler other tab settings windows scheduler
  24. After all settings are done, CLICK OK and your Python script is scheduled to run at the time you indicated. Keep track for the few initial runs to make sure everything is fine and you can leave it to run on its own thereafter.
  25. Happy batching and scheduling!
Want more information like this?

Similar Posts

  • |

    Quick Link to Connect to Google Analytics Developer API

    Hi Guys, My mentor and Godfather in IT,  Terry,  showed me a simple and quick link to connect to Google Analytics API.  Hope you also find it useful. Link is as below: https://ga-dev-tools.appspot.com/query-explorer/   To connect to your Google Analytics, Simply click on the link : https://ga-dev-tools.appspot.com/query-explorer/ Enter your login details for your Google Analytics account…

  • WindowsError: [Error 5] Access is denied: Anaconda Python Pip Installs and Upgrade

    I was trying to run a pip install ( pip install scikit-learn) and I encountered this error:

    This is the bit we are interested in ”

    ” if you look at the error it states “WindowsError” Hence the Access Denied error is due to not having the right level of privileges to carry…

  • Getting Stock Prices from Yahoo and plotting Python 3 Matplolib Urllib

    This is  some quick notes about getting stock data from Yahoo and plotting it using Matplotlib . The Python version used is Python 3.5 Credits to sentdex.  You can check him out on Youtube. In [11]:

      In [12]:

      In [13]:

      In [14]:

        Want more information like this?

  • |

    Python iloc, loc, ix Data Retrieving Selection Functions

      Pandas iloc, loc, and ix functions are very powerful ways to quickly select data from your dataframe. Today , we take a quick look at these 3 functions. Credits to Data School, you can check him out in Youtube  In [1]:

      In [2]:

      In [3]:

      Out[3]: City Colors Reported Shape Reported State…

  • |

    Test a Multiple – Multivariate Regression Model

    OVERVIEW My research work deals with Ghana, a country from the Gapminder dataset.   What I found in my multiple regression analysis. Discussion of the results for the associations between all of my explanatory variables and my response variable The primary quantitative explanatory variable in my regression analysis is the Income Per Person (incomeperperson) and…

Leave a Reply

Your email address will not be published. Required fields are marked *