Automate Python script using Task Scheduler on Windows

Automate Python script using Task Scheduler on Windows

Read how to set up schedules to run python scripts completely in the background without command prompt pop-up

STEP 1: Save python script file as .pyw extension

#script.pyw

your awesome code goes here...

STEP 2: Make python script into a batch file

**2.1. **Open notepad. Add the following and save it as .bat extension.

start "" "path_to_pythonw.exe" "path_to_python_script.pyw"

For example:

start "" "C:\Users\nihad\AppData\Local\Microsoft\WindowsApps\pythonw.exe" "C:\Users\nihad\Desktop\project\script.pyw"

Note that I used pythonw.exe and .pyw as python script extension here. This is to run python script in the background as well as to prevent/suppress the command prompt pop-up after every task execution. The start ensures the command prompt is closed after the batch file execution. The first empty "" is the title that should be used with the start. I just left it blank. After double-clicking on the .bat file, the python script should run.

Pro Tip💡: pythonw.exe should be in the same folder as python.exe, However, if you don't know the path of pythonw.exe, open CMD and execute where pythonw

where.jpg

No, I am not joking. Just fire it in the CMD😁

STEP 3: Set up task scheduler to run python at a desired time

3.1. Open task scheduler and in the action pane on the right side, click on Create Task.

index.jpg

3.2. Provide a name for the task. Then select Run whether user is logged on or not. This is to prevent or suppress the command prompt popping up after every task execution.

indexq.jpg

3.3. Navigate to Triggers tab and click on New. Upon clicking, a New Trigger wizard will pop up. I set up the trigger as scheduled to repeatedly run the task every 2 minutes daily. You can tweak these settings to suit your needs.

1 KWDf3nzBKmfm8gQZebozZw.png

3.4. Navigate to Actions tab and click on New. Upon clicking, a New Action wizard will pop up. Browse and choose the previously created batch file(in STEP 2).

1 Qlk3zv-E98DgTHwdYBjsxg.png

Finally, click OK in the new action and create task wizards.

You are good to go! You just automated the python script to run every 2 minutes in the background!