Forums

Code behaves differently in a scheduled task

I am running a scheduled task to generate new Markdown files for my web application, which is based on Flask and Flask-Flatpages. For this reason, the final line of my generation script restarts the web application with the following command:

os.utime('/var/www/walkerandreas_pythonanywhere_com_wsgi.py')

This works when I run the script manually; however, when I run it as a scheduled task, it gives me the following error:

Traceback (most recent call last):

File "/home/walkerandreas/mysite/content.py", line 184, in <module> os.utime('/var/www/walkerandreas_pythonanywhere_com_wsgi.py')

TypeError: utime() takes exactly 2 arguments (1 given)

Any idea why that might be the case and how to fix it?

That sounds like a Python version mismatch. In Python 2.7, utime takes two arguments, but in 3.x, the second argument is optional.

If you put an explicit Python interpreter into the command run by the scheduled task, it should fix it. So, for example, if you're currently running

/home/walkerandreas/something/myscript.py

and you want to make sure it's always run with Python 3.6, you can change the command to

python3.6 /home/walkerandreas/something/myscript.py

That was indeed the problem. Thank you very much for your help!

No problem -- glad to help!