ImportError: cannot import name 'CLOSED' from 'websockets.connection'

Hi everyone,

I’m currently deploying a Rasa chatbot to Okteto Cloud and I’m encountering this error:
ImportError: cannot import name ‘CLOSED’ from ‘websockets.connection’ (/usr/local/lib/python3.10/site-packages/websockets/connection.py)

Just to provide some context, I’m using Rasa version 3.5.1 and Python version 10.0.0. The error message appears when I run the ‘okteto up’ command.

I’ve attempted to fix the error by reinstalling the websockets library and checking all the dependencies to ensure they’re up to date, but the error still persists. I’m hoping someone might have some insight into what could be causing this issue or any suggestions on how to resolve it. Any help would be greatly appreciated. Thanks in advance!

hey @urbanusm , welcome to the community!

From the error it seems like it might be a syntax or versioning error. Do you have a repository or relevant code sample you can share to help us understand the issue you are running into?

Hey @ramiro , thanks for welcoming me to this amazing community!
Here is my okteto file:

name: real-estate

# The build section defines how to build the images of your development environment
# More info: https://www.okteto.com/docs/reference/manifest/#build
build:

  # You can use the following env vars to refer to this image in your deploy commands:
  #  - OKTETO_BUILD_RASA_ACTIONS_SERVER_REGISTRY: image registry
  #  - OKTETO_BUILD_RASA_ACTIONS_SERVER_REPOSITORY: image repo
  #  - OKTETO_BUILD_RASA_ACTIONS_SERVER_IMAGE: image name
  #  - OKTETO_BUILD_RASA_ACTIONS_SERVER_SHA: image tag sha256
  rasa-actions-server:
    context: .
    dockerfile: Dockerfile
    image: okteto.dev/rasa-server-bot:latest

  # You can use the following env vars to refer to this image in your deploy commands:
  #  - OKTETO_BUILD_RASA_SERVER_REGISTRY: image registry
  #  - OKTETO_BUILD_RASA_SERVER_REPOSITORY: image repo
  #  - OKTETO_BUILD_RASA_SERVER_IMAGE: image name
  #  - OKTETO_BUILD_RASA_SERVER_SHA: image tag sha256
  rasa-server:
    context: .
    dockerfile: Dockerfile
    image: okteto.dev/rasa-server-bot:latest

# The deploy section defines how to deploy your development environment
# More info: https://www.okteto.com/docs/reference/manifest/#deploy
deploy:
  compose:
    file: docker-compose.yml

# The dependencies section defines other git repositories to be deployed as part of your development environment
# More info: https://www.okteto.com/docs/reference/manifest/#dependencies
# dependencies:
#   - https://github.com/okteto/sample


# The dev section defines how to activate a development container
# More info: https://www.okteto.com/docs/reference/manifest/#dev
dev:
  rasa-actions-server:
    image: okteto.dev/rasa-server-bot:latest
    command:
      - bash
      - -c
      - rasa run actions
    sync:
      - actions:/app/actions
    forward:
      - 5055:5055
  rasa-server:
    image: okteto.dev/rasa-server-bot:latest
    command:
      - sh
      - -c
      - bash -c "rm -rf models/* && rasa train && rasa run --enable-api --cors \"*\" --debug"
    sync:
      - actions:/app/actions
      - data:/app/data
    forward:
      - 5006:5005

This is my docker file

FROM python:3.10.0-buster AS BASE

RUN apt-get update \
    && apt-get --assume-yes --no-install-recommends install \
        build-essential \
        curl \
        git \
        jq \
        libgomp1 \
        vim

WORKDIR /app

# upgrade pip version
RUN pip install --no-cache-dir --upgrade pip

RUN pip install rasa==3.5.1

ADD config.yml config.yml
ADD domain.yml domain.yml
ADD credentials.yml credentials.yml
ADD endpoints.yml endpoints.yml

And lastly here is my docker-compose file:

version: '4.17.1'
services:

  duckling-server:
    image: rasa/duckling:latest
    ports:
    - 8000:8000
    networks:
    - all

  rasa-server:
    image: rasa-server-bot:latest
    working_dir: "/app"
    build: "./"
    restart: always
    volumes:
    - ./actions:/app/actions
    - ./data:/app/data
    command: bash -c "rm -rf models/* && rasa train && rasa run --enable-api --cors \"*\" -p 8080"
    ports:
    - '5005:8080'
    public: true
    networks:
    - all

  rasa-actions-server:
    image: rasa-server-bot:latest
    working_dir: "/app"
    build: "./"
    restart: always
    volumes:
    - ./actions:/app/actions
    command: bash -c "rasa run actions"
    ports:
    - '5055:5055'
    networks:
    - all
    #security_opt:
    #- seccomp:unconfined

networks:
  all:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "true"

And this is how the error looks like:

  File "/usr/local/lib/python3.10/site-packages/sanic/server/protocols/websocket_protocol.py", line 3, in <module>
    from websockets.connection import CLOSED, CLOSING, OPEN
ImportError: cannot import name 'CLOSED' from 'websockets.connection' (/usr/local/lib/python3.10/site-packages/websockets/connection.py)
exit status 1
 x  Command execution failed: process exited with status 1

Hi @urbanusm !

Thanks for providing the code! I was able to reproduce and by googling the error I’ve found this stack-overflow post that suggests to pip install websockets==10.0, so I added ADD pip install websockets==10.0 and it did the trick

This is not the full solution though, as I’m now hitting some other error like ModuleNotFoundError: No module named 'actions'. I think this is due to the Dockerfile not copying anything like source files or requirements.txt for dependencies

I suggest you research more about Build your Python image and what files does rasa actually need, since there’s where these errors arise

Hi @AgustinRamiroDiaz ,

Thanks for responding! I checked out the StackOverflow post you mentioned and attempted to resolve the issue by downgrading my websockets version to 10.0, but unfortunately, even after adding ADD pip install websockets==10.0 to my Dockerfile, I still encountered the same error. I’m currently stuck and hoping to find a solution soon. Thank you.

Hi @urbanusm!

regarding the last proposal (ImportError: cannot import name 'CLOSED' from 'websockets.connection' - #4 by AgustinRamiroDiaz), you should add the instruction as RUN pip install websockets==10.0 instead of ADD pip install websockets==10.0. It seems to be a dependency issue of your app. Can you confirm that the Dockerfile is correct by trying to build and deploy a container with your app locally using docker build/run?