How to fix Python WebSocket TypeError: __init__() missing 3 required positional arguments: ‘environ’, ‘socket’, and ‘rfile’

Problem:

You are trying to connect a WebSocket in Python:

import websocket
ws = websocket.WebSocket()

but you see an error message like

TypeError                                 Traceback (most recent call last)
<ipython-input-3-5108525d0b43> in <module>
      1 import websocket
----> 2 ws = websocket.WebSocket()
      3 ws.connect("ws://192.168.1.211/ws")
      4 while True:
      5     result = ws.recv()

TypeError: __init__() missing 3 required positional arguments: 'environ', 'socket', and 'rfile'

Solution:

You have installed the wrong library ! You need to use websocket-client instead of websocket !

First, uninstall websocket:

pip uninstall websocket

Now install websocket-client:

pip install websocket-client

Now try again – your code should work now.