Python Websockets模块没有属性

use*_*014 12 python websocket

我有这个python文件,它向我的python-server发送websocket消息.但它不断出错,我之前看过这些错误,但我似乎无法解决它们我的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()
Run Code Online (Sandbox Code Playgroud)

错误

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'
Run Code Online (Sandbox Code Playgroud)

meh*_*hdy 28

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

$ pip install websocket-client
Run Code Online (Sandbox Code Playgroud)

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

  • 还有其他建议吗,我刚刚升级了,但还是没有运气 (2认同)
  • 再次尝试`pip uninstall websocket`和`pip install websocket-client` (2认同)

ske*_*r88 11

通过falsetru在这里查看答案:AttributeError:'module'对象没有属性'WebSocketApp'

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

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

  • 对我来说它是“websockets.py”。 (2认同)