2

I am trying to run some code that has 'import websocket' however I am getting the error: ModuleNotFoundError: No module named 'websocket'

I have Python 3.7.3 and I am running in Spyder (if that makes a difference).

So from other questions/answers I found on here, in my cmd I ran pip install websocket and then also pip install websocket-client when the first one didn't run.

I am still getting the ModuleNotFoundError. Does it matter the location/folder of the code or where I install the pip command in cmd?

My python code starts with these import statements:

import json
import websocket
import traceback
import helper
import ssl
import time as time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D

In cmd I ran:

C:\Users\myname>pip install websocket 

and also:

C:\Users\myname>pip install websocket-client

The error I am getting is:

File "C:/Users/micki/Downloads/Derbit-Volatility-Visulization-master/Derbit-Volatility-Visulization-master/Volatility Surface Class.py", line 2, in <module>
    import websocket

ModuleNotFoundError: No module named 'websocket'
sophros
  • 14,672
  • 11
  • 46
  • 75
user1781336
  • 85
  • 4
  • 12

3 Answers3

3

Not sure, as you did not cover how you installed and are using Spyder, though I think it is probably an issue with your environment. You might also find that you are missing the module "helper" as well. There's two easy options as follows:

  1. If you installed and are using Spyder via conda or anaconda, follow their documentation on installing websocket-client to the correct environment found here.
  2. The second option (the preferred option IMHO, as you can use any IDE or text editor going forward), regardless of how you installed Spyder, would be to create a python virtual environment python3 -m venv /path/to/new/virtual/environment, pip install all your dependencies in said environment, then link Spyder's interpreter to the interpereter that was installed when you made the environment. In Spyder, go to Tools -> Preferences -> Python interpreter -> check the "Use the following Python interpreter:" radio button and enter the path to the interpreter from the environment you just created. For reference, see docs on making and using a python venv here.
jacob
  • 828
  • 8
  • 13
  • I'm also curious what is the difference between websocket and websockets (with and "s")? Is there a difference? – user1781336 Oct 10 '19 at 19:44
  • @user1781336 Yes, `import websocket` will import [websocket-client](https://pypi.org/project/websocket_client/), while `import websockets` will import [websockets](https://pypi.org/project/websockets/). They're both APIs for harnessing the lower level functions of the websocket protocol. To understand the exact differences, you should read the docs. If you come up with something, post a question and answer it yourself to inform the community, as I am curious myself. – jacob Oct 10 '19 at 22:23
  • @user1781336 Did the answer help/work? If so, mark it as accepted; if not, clarify further... – jacob Oct 17 '19 at 16:47
0

If the websocket and websocket-client doesn't work, try:

pip install websocket_client
Arwildo
  • 826
  • 9
  • 8
  • Be careful when using pip and conda together!!!! https://stackoverflow.com/questions/56134588/is-that-a-bad-idea-to-use-conda-and-pip-install-on-the-same-environment – Evan Rosica Jan 21 '22 at 04:55
0

This solved my issue:

sudo pip install websocket-client
Charles P.
  • 1,947
  • 16
  • 13