Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'create_connection'

Tags:

python-3.x

I am using Python 3.4 and websocket-client 0.44. I am trying a Python webscoket script to fetch stream data from Bitfinex using socket. Here is my script which which i have written:

import json

from websocket import create_connection

ws = create_connection("wss://api.bitfinex.com/ws/2")
#ws.connect("wss://api2.bitfinex.com:3000/ws")
ws.send(json.dumps({
    "event": "subscribe",
    "channel": "book",
    "symbol": "tBTCUSD",
}))


while True:
    result = ws.recv()
    result = json.loads(result)
    xxx = result
    print(xxx)
    #print(result[1])

ws.close()

and what i am getting is, error:

Traceback (most recent call last):
  File "D:/bitstamp/socket.py", line 3, in <module>
    from websocket import create_connection
  File "C:\Python34\lib\site-packages\websocket_client-0.44.0-py3.4.egg\websocket\__init__.py", line 23, in <module>
    from ._app import WebSocketApp
  File "C:\Python34\lib\site-packages\websocket_client-0.44.0-py3.4.egg\websocket\_app.py", line 35, in <module>
    from ._core import WebSocket, getdefaulttimeout
  File "C:\Python34\lib\site-packages\websocket_client-0.44.0-py3.4.egg\websocket\_core.py", line 24, in <module>
    import socket
  File "D:\bitstamp\socket.py", line 3, in <module>
    from websocket import create_connection
ImportError: cannot import name 'create_connection'

Is there an issue in Python 3.4 or am I doing this incorrectly?

like image 309
Sahadev Avatar asked Nov 17 '17 06:11

Sahadev


3 Answers

There is nothing wrong with Python 3.4. It seems you may be missing the websocket-client package.

Please install this package by running the command in your terminal as shown below and that should solve your issue.

pip install websocket-client
like image 89
Kapil Marwaha Avatar answered Nov 03 '22 00:11

Kapil Marwaha


Don't call your script socket.py. There is already a socket in Python's standard library, and you are conflicting with it. You can tell that this is the problem by following the traceback: It starts in your script, descends into websocket, eventually tries to import socket, and gets your script again.

like image 5
Kevin Avatar answered Nov 02 '22 23:11

Kevin


you should first install websocket-client, or install websocket-client after uninstalling websocket.

like image 3
towee Avatar answered Nov 03 '22 00:11

towee