Reputation: 11
I have a simple Python script that runs correctly when I launch it in Jupyter Notebook or after I convert it to .exe, but doesn't work when I launch it via Windows Scheduler (I tried to launch both the .py file and the .exe). What am I missing?
Background:
I have a simple 3.7 Python script which I would like to run at regular intervals. I am testing the approach with a simple Python script before moving to a more complex script.
The script I have works well when I launch it in Jupyter, but it doesn't work when using Windows Scheduler. What I did was, I created a basic task in Windows scheduler, and set the following properties:
When I click on RUN task in Windows Scheduler, a command-like, black background windows appears for a few seconds, then it disappears, but nothing happens and the output txt file doesn't get edited. I get no error messages, and when I check the "History" of the task, it says "Task completed" or "Task registration updated". Basically, there seems to be no error, and yet, nothing happens.
I'd like to solve the issue using Windows Scheduler but also I am open to suggestions on ways to run the scripts regularly. However, I am a very basic Python user and may not be able to do more complex things. My system is a Windows 10 Home, Python 3.7.
Simple Code I am using:
import datetime
now = datetime.datetime.now()
file = open("testfile.txt","w")
file.write("Hello Word" + str(now))
print("done")
Note: The script writes the datestamp in a text file in the same folder of the .ipynb file. I have also converted the file to an .exe using pyinstaller. in both cases the script works well. However, it doesn't work with Windows scheduler.
Upvotes: 1
Views: 1542
Reputation: 583
The Windows Task Scheduler, in my experience, does not play well with Python. Try creating a .bat file with the following:
cmd full\path\to\python.exe "full\path\to\script.py" > output.log 2>&1
This is a batch file that will open cmd.exe, and execute your script on your python executable. It will then redirect the STDOUT and STDERR to file so you can see what went wrong.
Tell Task Scheduler to run this batch file (with no other arguments) instead of the Python script, and see what goes to the log. Either it will run fine with the batch file, or you will have more info with which to troubleshoot.
Upvotes: 1