1

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:

  • general: run only when user is logged in;
  • triggers: daily;
  • action: I have tried several combinations of inputs. The latest combo I tried is to have in "program/script" C:\Pythondata\dist\test.exe and no optional argument;
  • conditions and settings: I kept the default options.

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Dario
  • 11
  • 2
  • did you install python via anaconda? I have a similar script that works and can share it to you – jose_bacoy May 21 '19 at 21:30
  • Had a tough time with that kind of scheduling. You might have a look at the schedule library https://pypi.org/project/schedule/ which handles that smoothly – Sebastien D May 21 '19 at 22:00
  • I don't know about windows, but in linux if you don't specify an absolute path when writing a file, the path is interpreted as being relative to your current directory. This applies to crontab (linux's task automation tool) too, but is complicated by the fact that it's not clear where crontab 'is' when it runs the script. This is a common pitfall with automating tasks, at least in linux. I would recommend doing a search for the filename. You might find it where you don't expect it. – ibonyun May 21 '19 at 22:01
  • It's for Windows 7, so the procedure for setting environment variables is probably somewhat different, but otherwise I think what's in [my answer](https://stackoverflow.com/a/4209102/355230) to another Windows-related question might be relevant. Also note that you should add a newline to the data written: `file.write("Hello Word" + str(now) + "\n")` and close the file at the afterwards with a`file.close()`. – martineau May 21 '19 at 22:39
  • `open("testfile.txt","w")` -- This creates the file in the working directory, which depends on how the process is run. It is not "in the same folder of the .ipynb file". For the script directory, if it's a frozen executable (i.e. if `sys.frozen` is defined), use the directory of `sys.executable`, else use the directory of `__file__`. – Eryk Sun May 22 '19 at 01:33

1 Answers1

1

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.

Triggernometry
  • 573
  • 2
  • 9