pythondjangodjango-rest-frameworkdaphne

Daphne ModuleNotFoundError: No module named


I'm unable to get the Daphne server running. It always fails with the following error:

 Traceback (most recent call last):
   File "/usr/local/bin/daphne", line 8, in <module>
     sys.exit(CommandLineInterface.entrypoint())
   File "/usr/local/lib/python3.8/site-packages/daphne/cli.py", line 171, in entrypoint
     cls().run(sys.argv[1:])
   File "/usr/local/lib/python3.8/site-packages/daphne/cli.py", line 233, in run
     application = import_by_path(args.application)
   File "/usr/local/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
     target = importlib.import_module(module_path)
   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
 ModuleNotFoundError: No module named 'apps'

/home/prassel/apps/asgi.py

django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "websocket": URLRouter([path('ws/notifications/', NotificationConsumer.as_asgi())]),
    }
)

I'm starting Daphne as follows from /home/prassel/docker-compose.yml

  daphne:
    platform: linux/amd64
    build:
      context: .
      dockerfile: Dockerfile.daphne
    command: 'sh -c "daphne -b 0.0.0.0 -p 8000  apps.asgi:application"'
    restart: always
    working_dir: /home/prassel

Dockerfile.daphne

FROM python:3.8-buster

ENV PYTHONUNBUFFERED 1

ARG REQUIREMENTS_FILE

WORKDIR /home/prassel/

RUN pip install daphne

Solution

  • UPDATE: You're not adding your project files to your Docker container during the build, so your container doesn't have anything to run. You need to add them using a COPY or an ADD command.

    It's one of the basics of Docker. You should do some reading on what are Docker containers and how they work, or you can use an official example or follow one of a countless Django + Docker tutorials available on the web.


    I believe the issue lies within your Docker setup. Make sure you set WORKDIR in your Dockerfile.daphne and working_dir: in your docker-compose.yml to the same value, and you should be good.

    Dockerfile:

    FROM ...
    
    WORKDIR /yourproject
    
    COPY ...
    
    RUN ...
    
    ...
    
    ...
    

    docker-compose.yml:

      daphne:
        platform: linux/amd64
        build:
          context: .
          dockerfile: Dockerfile.daphne
        command: 'sh -c "daphne -b 0.0.0.0 -p 8000  apps.asgi:application"'
        restart: always
        working_dir: /yourproject