簡體   English   中英

websocket-client 不適用於 Python3/VSCode

[英]websocket-client will not work on Python3/VSCode

我正在嘗試在 Windows 10 上使用帶有 VSCode 的 Python 3.10 中的 websocket-client 包。我能夠在帶有 Python 3.10.1 的 Ubuntu 20.04 VM 上運行完全相同的代碼

這是我的 websocket 客戶端版本:

Name: websocket-client
Version: 1.2.3
Summary: WebSocket client for Python with low level API options
Home-page: https://github.com/websocket-client/websocket-client.git
Author: liris
Author-email: liris.pp@gmail.com
License: Apache-2.0
Location: c:\users\dazk2\appdata\local\programs\python\python310\lib\site-packages
Requires:
Required-by:

我得到錯誤:

Traceback (most recent call last):
  File "c:\...\websocket.py", line 32, in <module>
    ws = websocket.WebSocketApp("ws://localhost:8765",
AttributeError: module 'websocket' has no attribute 'WebSocketApp'. Did you mean: 'websocket'?

這是代碼:(這不是我正在處理的,但我嘗試了示例代碼,但它仍然沒有工作)

import websocket
import _thread as thread
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, a, b):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
#    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://localhost:8765",
                                 on_message = on_message,
                                 on_error = on_error,
                                 on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()    

您的文件名為websocket.py ,這將覆蓋正在導入websocket的 websocket 庫導入。 本質上,您正在做的是將文件導入自身,並且由於文件本身沒有WebSocketApp調用,因此它崩潰了。 盡管 Python 正確檢測到它存在於websocket庫中(基於崩潰建議),但由於兩者使用相同的名稱,這會導致沖突。

如果您將文件重命名為不同的名稱,則庫導入應按預期工作,並且您的編碼應該運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM