Troubleshooting Task Scheduler Running a Python Script

program: Python
package manager: PIP
ide: Spyder

The Background

Creating an automated or scheduled task on your computer in Windows or Mac is pretty awesome for saving time doing routine tasks, particularly routine scripts or program runs. However, when setting up the task in the Task Scheduler (Windows), there isn’t much of a GUI that shows what is happening with the program like there would be in an IDE.

I was setting up a Python script to automate. To complicate things, I use Spyder IDE to code in.

The Problem

I created a *.bat file. Double clicked on the file to run the script, and a window popped up and quickly closed.

I doubled checked in Spyder to see if the program ran, sure enough, ran like a charm.

The Solutions

Yes, plural solutions. There were lots of things wrong.

First

The biggest problem was that Spyder seems to have it’s own package folder that has some built in packages like Numpy, Matplotlib, and Pandas. I’m sure there are others. So, when I run my program in Spyder, everything ran great. But when I ran it using my *.bat file, it was running without the IDE and therefore didn’t have the built in scripts.

To figure this out, I opened the *.py file (my program) using IDLE, then ran (F5). The shell told me:

import numpy
ModuleNotFoundError: No module named ‘numpy’

Second

pip install numpy

That’s the short answer.

The long answer is to systematically install each package that had been assumed to be installed in Python.

This is done by continuing to run the Python script to see why it crashes. This will happen with similar ‘ModuleNotFoundError’s as seen above. Each module not found, pip install that package.

pip install <package name>

The above command is typed directly into Command Prompt, if pip has been installed correctly.

One the program runs completely within IDLE, it should now run smoothly in the Task Scheduler.

Third

Setting up a Task Scheduler might be a bit confusing. It’s an app or program on Windows now of the same name. It’s also found in the directory:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

When you click create task you will be stepped through a few tabs to create the new task.

Name the task, select whether you need to be logged in for it to run or not, set your triggers which can be time of day or specific days, and action is the tab that holds the information for what program to run.

Create a *.bat File

Head to this other post I’ve made on How To Create a *.bat File in Windows For Task Scheduler To Run Python Script. It should give you all the details.

Creating a *.bat file to run your script is probably the easiest way to create your action in the Task Scheduler. Once your *.bat file is created, I would suggest going to the actual *.bat file and double clicking on it to confirm that it does work.

tip: insert some print code or a sleep() statement at the end of your code so that you can get a confirmation that it has actually run. Sometimes the programs run pretty quick and you don’t get any feedback that it’s run.

If your *.bat file runs properly, in the Action tab of the Task, add a new action, and browse to the *.bat file you created above and select ‘start a program’ as the action you’d like to accomplish.

This should now show up as a task in your task scheduler. To see if it’s going to work before you wait for your triggers to be hit, right click on the task and select run.

Hope this helps you troubleshoot your scheduled task in Windows.

1 Comment

Leave a Reply

13 + 5 =