Forums

Error importing websocket

I have a websocket stream which I want to save data from in a .csv file. The code works fine, but since my goal is to have it run 24/7, I am implementing the method described on the Long Running Tasks help page.

However, I get an error when the scheduled task tries to run my script. The log says that the error came from trying to import websocket.

So I wrote a simple script to try to try to find out what is going wrong. The code is below. It simply saves the current time to a .csv file every 15 seconds. Using the code in the Long Running Tasks help page, I have a scheduled task which attempts to run saveTime() from the timeprint.py file once an hour.

timeprint.py

import csv
import time
import websocket     # runs fine when this line is commented out

def saveTime():
    while True:
        with open('longprint.csv', 'a') as myFile:
            csv.writer(myFile, delimiter = ',').writerow([int(time.time())])
        time.sleep(15)

I first tried running it with line 3 (import websocket) commented out. It ran without any problems for roughly a day. Then, when I tried running it without commenting out line 3, I get the following error in the scheduled task log...

log

Traceback (most recent call last):
File "/home/arbiter/myScheduledTask.py", line 5, in <module>
    from timeprint import saveTime
File "/home/arbiter/timeprint.py", line 3, in <module>
    import websocket
ImportError: No module named websocket

In short: the code which my scheduled task is attempting to run cannot import a module. Does anyone have any ideas on to why this might be happening?

Oh sonuvabitch, somehow I missed this question when I was looking through the forum.

Having the the scheduled task run python3.6 /home/arbiter/myScheduledTask.py instead of just /home/arbiter/myScheduledTask.py fixed the problem. Thanks @giles!

great! alternatively, use make your file executable and use hashbangs to control which version of python is run etc.