Forums

Scheduled task has an error

I'm trying to schedule a section of my flask code daily (it sends me an email with the weather) and when I run it normally it's fine but when scheduled it gives an error. The scheduled code:

import sys

sys.path.insert(0, '/home/JackGrimm/mysite')

import flask_app as importfile

def main():

importfile.sendweather()

main()

The error:

File "/home/JackGrimm/mysite/flask_app.py", line 4, in <module>

from urllib.request import urlopen

ImportError: No module named request

That section of code is used for another function, so is there a simple fix other than splitting up my code? It's only giving an error when I schedule it.

  1. what python version are you running- could this be a problem due to it running with a diff python version?
  2. is it because you are in a different working directory and so there is a particular module that you could import that you cannot import now?

Everything I've been running is in Python 3.6 (although I don't know how to check if the scheduled task is python 3.6 too)

The whole error log is:

Traceback (most recent call last):

File "/home/JackGrimm/mysite/dailyweather.py", line 4, in <module>

import flask_app as importfile

File "/home/JackGrimm/mysite/flask_app.py", line 4, in <module>

from urllib.request import urlopen

ImportError: No module named request

2018-08-27 11:39:12 -- Completed task, took 7.00 seconds, return code was 1.

I've moved everything under /home/JackGrimm/mysite and it still gives the error when scheduled, but runs normally when I run it.

I solved it by forcing the schedule to run it in 3.6 I assumed that it was the default.

great! glad you resolved it. It's always better to be explicit about this type of stuff!