简体   繁体   English

AttributeError: 'module' object 没有属性 'WebSocketApp'

[英]AttributeError: 'module' object has no attribute 'WebSocketApp'

I'm trying to connect to an API with python, using WebSocketApp, and I cannot seem to do it.我正在尝试使用 WebSocketApp 连接到 API 和 python,但我似乎做不到。 No matter what I try, I keep getting this error:无论我尝试什么,我都会不断收到此错误:

AttributeError: 'module' object has no attribute 'WebSocketApp' AttributeError: 'module' object 没有属性 'WebSocketApp'

Here is the simple code I am using这是我正在使用的简单代码

import websocket
import json

def on_open(ws):
    json_data = json.dumps({'data':'value'})
    ws.send(json_data)

def on_message(ws, message):
    print('data update: %s' % message)

if __name__ == "__main__":
    apiUrl = "appurl"
    ws = websocket.WebSocketApp(apiUrl, on_message = on_message, on_open = on_open)
    ws.run_forever()

Any help will be greatly appreciated, all the solutions I have found online have not worked for me.任何帮助将不胜感激,我在网上找到的所有解决方案都不适合我。

It looks like you're trying to use features of the websocket-client package and not just the websocket package. 看起来您正在尝试使用websocket-client软件包的功能,而不仅仅是websocket软件包。 Pip-install websocket-client from the command line (or install it using whatever other package manager you have), then try running your code again. 从命令行websocket-client -install websocket-client (或使用你拥有的任何其他软件包管理器安装它),然后再次尝试运行你的代码。

pip install websocket-client

Make sure that you didn't name your file as websocket.py ; 确保您没有将文件命名为websocket.py ; Otherwise, it will prevent import of the desired third-party module websocket ; 否则,它将阻止导入所需的第三方模块websocket ; because your module is searched first according to sys.path module search path. 因为首先根据sys.path模块搜索路径搜索模块。

Rename your module to other name, and make sure to clean websocket.pyc if there it is. 将模块重命名为其他名称,并确保清除websocket.pyc如果有)。

In this case, You should uninstall the possible inconsistent previous versions and install websocket-client again: 在这种情况下,您应该卸载可能不一致的先前版本并再次安装websocket-client:

pip uninstall websocket-client
pip uninstall websocket

an then install a new version: 然后安装新版本:

pip install websocket-client

The issue can be generated because the version.问题可能是因为版本。 Version v1.3 and above does me the same problem but installing v1.2.3 the issue disappeared... v1.3 及以上版本对我有同样的问题,但安装 v1.2.3 问题消失了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM