Forums

Error Importing Websocket

I have a code which is written in python3. 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.

When I try to run a simple script to find out what is going wrong, I get the same error. The code is below. It simply saves the current time to a .csv file every 15 seconds.

timeprint.py

import csv
import time
import websocket

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

log

Traceback (most recent call last):
File "timeprint.py", line 3, in <module>
   import websocket     
ModuleNotFoundError: No module named 'websocket'

My specific code is written in python3 and therefore can't be executed in python2. Also when I try to run it in python3.3 or python3.6, I get the same error.

(I already installed "pip install websocket --user")

Any ideas to why this happening?

Ok... I find out immediately after i posted this.

Trying to install websocket for python3, I have to install "pip3 install websocket --user".

Now it's working fine.

Yup, that's the solution. I'd actually recommend being even more specific -- use the full version of Python when you install, for example, pip3.6 install --user websocket.

One warning -- websocket connections to other sites won't generally work from inside a free account :-(

Hi Giles

Thanks for telling.

Would the websocket connections work with an upgraded account like the hacker package? Are the API connections unlimited?

Hi Shapoy,

Yes, Websockets will work find from a paid account, and paid accounts get unrestricted Internet access -- you can connect to any API out there.

(Of course, they might block you or block connections from us, but the only problem like that that I am aware of is that Instragram have been blocking some people who were using us to run bots that auto-followed people and auto-liked photos and that kind of thing.)