skip to Main Content

I don’t know how to fix this error on Ubuntu.
I’m trying to run a FastApi server using uvicorn

uvicorn.run("api:app", host="0.0.0.0", port=3100, reload=True, workers=10)

I’ve tried pip3 install websockets :

Collecting websockets
  Downloading https://files.pythonhosted.org/packages/7e/86/cef054220bc080451fe9663ce7f99beda0599098241190b6b6dc1073ab92/websockets-10.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (112kB)
    100% |████████████████████████████████| 112kB 5.9MB/s 
Installing collected packages: websockets
Successfully installed websockets-8.1

I don’t know what else needs to be installed

    Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
    return future.result()
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/server.py", line 67, in serve
    config.load()
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/config.py", line 471, in load
    ws_protocol_class = import_from_string(WS_PROTOCOLS[self.ws])
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/importer.py", line 24, in import_from_string
    raise exc from None
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/protocols/websockets/auto.py", line 17, in <module>
    from uvicorn.protocols.websockets.websockets_impl import WebSocketProtocol
  File "/home/hafid/.local/lib/python3.7/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 9, in <module>
    from websockets.datastructures import Headers
ModuleNotFoundError: No module named 'websockets.datastructures'

3

Answers


  1. Chosen as BEST ANSWER

    needed to update setuptools and pip

    pip3 install -U setuptools

    pip3 install -U pip


  2. I had the same problem and i’m not sure why this happened but i’ll atleast show you my workaround.

    The best thing to do would be running:

    pip uninstall websockets

    pip install websockets

    But for some reason this command could not uninstall it for me, so i did it manually.

    Since u are in Ubuntu open this folder as root:

    /usr/local/lib/python3.8/dist-packages/websocket

    Then u can manually download the files at websockets current version files

    In my case i noticed that in my old websockets folder i didn’t have websockets.datastructures and also some other files such as the folders legacy and extensions so i just unzipped the whole library to the path cited above and it worked.

    Login or Signup to reply.
  3. To me, any of the two other answers work. I just upgrade websockets with:

    pip install -U websockets
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search