LoginSignup
5
1

More than 3 years have passed since last update.

websocketとwebsocket_clientの競合でうまくimportできないのを解決する

Posted at

達成したいこと

  • import websocketしたい
  • websocket.create_connection()したい

やったこと

importできない

sumple_ws.py
from websocket import create_connection

Traceback (most recent call last):
  File "sumple_ws.py", line 10, in <module>
    from websocket import create_connection
ImportError: cannot import name 'create_connection' from 'websocket' (unknown location)

:thinking:

websocket_clientとかいうやつがある

サーバ側とクライアント側で使うべきパッケージが違うらしい

今回のケースでは誤り

$  pip install websocket

こっちが正解

$  pip install websocket-client

なのにimportするときはどっちもwebsocket
リーダブルコード読んできてください

websocket_clientを導入したのにimportできない

はい完璧

$  pip install websocket-client
$  pip uninstall websocket

$  pip freeze | grep websocket
websocket-client==0.57.0

が 再現

sumple_ws.py
from websocket import create_connection
Traceback (most recent call last):
  File "sumple_ws.py", line 10, in <module>
    from websocket import create_connection
ImportError: cannot import name 'create_connection' from 'websocket' (unknown location)

import websocketの名前空間がおかしい

対話モードで確認

>>> import websocket
>>> dir(websocket)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

:thinking: :thinking: :thinking:

websocket_clientも再インストールする

$  pip uninstall websocket-client
$  pip install websocket-client

対話モード
```python
>>> import websocket
>>> dir(websocket)
['ABNF', 'DEFAULT_SOCKET_OPTION', 'STATUS_ABNORMAL_CLOSED', 'STATUS_BAD_GATEWAY', 'STATUS_GOING_AWAY', 'STATUS_INVALID_EXTENSION', 'STATUS_INVALID_PAYLOAD', 'STATUS_MESSAGE_TOO_BIG', 'STATUS_NORMAL', 'STATUS_POLICY_VIOLATION', 'STATUS_PROTOCOL_ERROR', 'STATUS_STATUS_NOT_AVAILABLE', 'STATUS_TLS_HANDSHAKE_ERROR', 'STATUS_UNEXPECTED_CONDITION', 'STATUS_UNSUPPORTED_DATA_TYPE', 'WebSocket', 'WebSocketAddressException', 'WebSocketApp', 'WebSocketBadStatusException', 'WebSocketConnectionClosedException', 'WebSocketException', 'WebSocketPayloadException', 'WebSocketProtocolException', 'WebSocketProxyException', 'WebSocketTimeoutException', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_abnf', '_app', '_cookiejar', '_core', '_exceptions', '_handshake', '_http', '_logging', '_socket', '_ssl_compat', '_url', '_utils', 'continuous_frame', 'create_connection', 'debug', 'dump', 'enableTrace', 'error', 'frame_buffer', 'getdefaulttimeout', 'isEnabledForDebug', 'isEnabledForError', 'isEnabledForTrace', 'recv', 'recv_line', 'send', 'setdefaulttimeout', 'sock_opt', 'trace', 'warning']

今度こそ完璧

要因

インストールとアンインストールのタイミングのせいでなんやかんやあったと思われる

これが

$  pip install websocket-client
$  pip uninstall websocket

この順番ならよかったはず

$  pip uninstall websocket
$  pip install websocket-client
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1