Python Websockets模块没有属性

时间:2016-10-24 06:50:05

标签: python websocket

我有这个python文件,它将websocket消息发送到我的python-server。但它不断出现错误,我之前看到过这些错误,但我似乎无法修复它们我的python版本是python2.7

#!/usr/bin/python
import websocket
import sys
val = sys.argv[1]
ws = websocket.create_connection("ws://ipaddress:9001")
ws.send(val)
ws.close()

错误

Traceback (most recent call last):
  File "./test.py", line 5, in <module>
    ws = websocket.create_connection("ws://ipaddress:9001")
AttributeError: 'module' object has no attribute 'create_connection'

2 个答案:

答案 0 :(得分:21)

你安装了错误的库(websocket) 尝试安装websocket-client

$ pip install websocket-client

然后你的代码必须正常工作

答案 1 :(得分:8)

在这里通过falsetru查看答案:AttributeError: 'module' object has no attribute 'WebSocketApp'

” 确保您没有将文件命名为websocket.py;否则,它将阻止导入所需的第三方模块websocket;因为首先根据sys.path模块搜索路径搜索模块。

将模块重命名为其他名称,并确保在生成时清除websocket.pyc。“