daphne: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings...

created at 06-17-2021 views: 8

error description

I got this error from my daphne logs after deployment, but I'm sure I have configured all the required paths and environment variables. 

for example, the frequently mentioned problem in google: setting the DJANGO_SETTINGS_MODULE as well as adding the django.setup() function to make sure all the apps loaded already in asgi.py:

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
django.setup()

However, I still got the same error:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings...

solution

Finally, I realized that the sequence of configuration statements may be the problem——I haven't even doubt this before because my project was initially generated by Cookiecutter.

So the problem is we should move the statement of DJANGO_SETTINGS_MODULE to the head of our script:

import os
import sys
import django


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
django.setup()

from pathlib import Path
from channels.routing import ProtocolTypeRouter, URLRouter
...

then the DJANGO_SETTINGS_MODULE variable should be read properly.

restart your project and check your logs.

So happy to have this problem solved, just record for those who have the same problem as mine.

Please log in to leave a comment.