Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. Heroku

Ask Question

I am trying to set up my Django project on Heroku. It's my first time doing so.

When deploying it succeeds but when opening up the view I get this message: ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. (full traceback below).

I am using Heroku Postgres and Heroku Redis add-ons.

What am I doing wrong?

Procfile
release: python manage.py migrate
web: daphne test_cs.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker channels --settings=test_cs.settings -v2
runtime.txt
python-3.10.2
asgi.py
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import main.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_cs.settings')
application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            main.routing.websocket_urlpatterns
Full traceback
2022-02-14T22:14:52.566118+00:00 heroku[release.1702]: State changed from starting to up
2022-02-14T22:14:55.258559+00:00 app[release.1702]: Operations to perform:
2022-02-14T22:14:55.258595+00:00 app[release.1702]: Apply all migrations: admin, auth, contenttypes, main, sessions, social_django
2022-02-14T22:14:55.283487+00:00 app[release.1702]: Running migrations:
2022-02-14T22:14:55.283489+00:00 app[release.1702]: No migrations to apply.
2022-02-14T22:14:55.877375+00:00 heroku[release.1702]: Process exited with status 0
2022-02-14T22:14:55.963114+00:00 heroku[release.1702]: State changed from up to complete
2022-02-14T22:14:59.550199+00:00 heroku[web.1]: State changed from crashed to starting
2022-02-14T22:15:05.339497+00:00 heroku[web.1]: Starting process with command `daphne test_cs.asgi:application --port 47797 --bind 0.0.0.0 -v2`
2022-02-14T22:15:06.845859+00:00 app[web.1]: Traceback (most recent call last):
2022-02-14T22:15:06.845875+00:00 app[web.1]: File "/app/.heroku/python/bin/daphne", line 8, in <module>
2022-02-14T22:15:06.845948+00:00 app[web.1]: sys.exit(CommandLineInterface.entrypoint())
2022-02-14T22:15:06.845962+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/cli.py", line 170, in entrypoint
2022-02-14T22:15:06.846046+00:00 app[web.1]: cls().run(sys.argv[1:])
2022-02-14T22:15:06.846048+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/cli.py", line 232, in run
2022-02-14T22:15:06.846144+00:00 app[web.1]: application = import_by_path(args.application)
2022-02-14T22:15:06.846145+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/utils.py", line 12, in import_by_path
2022-02-14T22:15:06.846213+00:00 app[web.1]: target = importlib.import_module(module_path)
2022-02-14T22:15:06.846221+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
2022-02-14T22:15:06.846301+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-02-14T22:15:06.846310+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-02-14T22:15:06.846387+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-02-14T22:15:06.846443+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-02-14T22:15:06.846499+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-02-14T22:15:06.846546+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-02-14T22:15:06.846592+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-02-14T22:15:06.846632+00:00 app[web.1]: File "/app/./test_cs/asgi.py", line 8, in <module>
2022-02-14T22:15:06.846690+00:00 app[web.1]: import main.routing
2022-02-14T22:15:06.846691+00:00 app[web.1]: File "/app/./main/routing.py", line 3, in <module>
2022-02-14T22:15:06.846744+00:00 app[web.1]: from . import consumers
2022-02-14T22:15:06.846745+00:00 app[web.1]: File "/app/./main/consumers.py", line 6, in <module>
2022-02-14T22:15:06.846790+00:00 app[web.1]: from .models import Messages, CustomUser
2022-02-14T22:15:06.846798+00:00 app[web.1]: File "/app/./main/models.py", line 3, in <module>
2022-02-14T22:15:06.846844+00:00 app[web.1]: from django.contrib.auth.models import AbstractUser
2022-02-14T22:15:06.846845+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
2022-02-14T22:15:06.846892+00:00 app[web.1]: from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
2022-02-14T22:15:06.846900+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
2022-02-14T22:15:06.846956+00:00 app[web.1]: class AbstractBaseUser(models.Model):
2022-02-14T22:15:06.846957+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 108, in __new__
2022-02-14T22:15:06.847019+00:00 app[web.1]: app_config = apps.get_containing_app_config(module)
2022-02-14T22:15:06.847027+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
2022-02-14T22:15:06.847114+00:00 app[web.1]: self.check_apps_ready()
2022-02-14T22:15:06.847115+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/registry.py", line 135, in check_apps_ready
2022-02-14T22:15:06.847172+00:00 app[web.1]: settings.INSTALLED_APPS
2022-02-14T22:15:06.847180+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/conf/__init__.py", line 84, in __getattr__
2022-02-14T22:15:06.847236+00:00 app[web.1]: self._setup(name)
2022-02-14T22:15:06.847244+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/conf/__init__.py", line 65, in _setup
2022-02-14T22:15:06.847293+00:00 app[web.1]: raise ImproperlyConfigured(
2022-02-14T22:15:06.847331+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2022-02-14T22:15:07.091182+00:00 heroku[web.1]: Process exited with status 1
2022-02-14T22:15:07.150610+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-14T22:15:07.157102+00:00 heroku[web.1]: State changed from crashed to starting
2022-02-14T22:15:12.787590+00:00 heroku[web.1]: Starting process with command `daphne test_cs.asgi:application --port 6813 --bind 0.0.0.0 -v2`
2022-02-14T22:15:14.286407+00:00 app[web.1]: Traceback (most recent call last):
2022-02-14T22:15:14.286427+00:00 app[web.1]: File "/app/.heroku/python/bin/daphne", line 8, in <module>
2022-02-14T22:15:14.286487+00:00 app[web.1]: sys.exit(CommandLineInterface.entrypoint())
2022-02-14T22:15:14.286496+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/cli.py", line 170, in entrypoint
2022-02-14T22:15:14.286573+00:00 app[web.1]: cls().run(sys.argv[1:])
2022-02-14T22:15:14.286582+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/cli.py", line 232, in run
2022-02-14T22:15:14.286658+00:00 app[web.1]: application = import_by_path(args.application)
2022-02-14T22:15:14.286661+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/daphne/utils.py", line 12, in import_by_path
2022-02-14T22:15:14.286710+00:00 app[web.1]: target = importlib.import_module(module_path)
2022-02-14T22:15:14.286712+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
2022-02-14T22:15:14.286777+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-02-14T22:15:14.286785+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-02-14T22:15:14.286857+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-02-14T22:15:14.286897+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-02-14T22:15:14.286937+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-02-14T22:15:14.286976+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-02-14T22:15:14.287024+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-02-14T22:15:14.287063+00:00 app[web.1]: File "/app/./test_cs/asgi.py", line 8, in <module>
2022-02-14T22:15:14.287111+00:00 app[web.1]: import main.routing
2022-02-14T22:15:14.287120+00:00 app[web.1]: File "/app/./main/routing.py", line 3, in <module>
2022-02-14T22:15:14.287170+00:00 app[web.1]: from . import consumers
2022-02-14T22:15:14.287172+00:00 app[web.1]: File "/app/./main/consumers.py", line 6, in <module>
2022-02-14T22:15:14.287217+00:00 app[web.1]: from .models import Messages, CustomUser
2022-02-14T22:15:14.287225+00:00 app[web.1]: File "/app/./main/models.py", line 3, in <module>
2022-02-14T22:15:14.287267+00:00 app[web.1]: from django.contrib.auth.models import AbstractUser
2022-02-14T22:15:14.287275+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
2022-02-14T22:15:14.287328+00:00 app[web.1]: from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
2022-02-14T22:15:14.287330+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
2022-02-14T22:15:14.287388+00:00 app[web.1]: class AbstractBaseUser(models.Model):
2022-02-14T22:15:14.287390+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 108, in __new__
2022-02-14T22:15:14.287452+00:00 app[web.1]: app_config = apps.get_containing_app_config(module)
2022-02-14T22:15:14.287460+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
2022-02-14T22:15:14.287542+00:00 app[web.1]: self.check_apps_ready()
2022-02-14T22:15:14.287551+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/registry.py", line 135, in check_apps_ready
2022-02-14T22:15:14.287615+00:00 app[web.1]: settings.INSTALLED_APPS
2022-02-14T22:15:14.287617+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/conf/__init__.py", line 84, in __getattr__
2022-02-14T22:15:14.287676+00:00 app[web.1]: self._setup(name)
2022-02-14T22:15:14.287685+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/conf/__init__.py", line 65, in _setup
2022-02-14T22:15:14.287742+00:00 app[web.1]: raise ImproperlyConfigured(
2022-02-14T22:15:14.287782+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2022-02-14T22:15:14.516624+00:00 heroku[web.1]: Process exited with status 1
2022-02-14T22:15:14.569469+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-14T22:15:44.023293+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=name.herokuapp.com request_id=1235a703-498b-416b-9931-42394f8de79d fwd="217.159.161.125" dyno= connect= service= status=503 bytes= protocol=https
2022-02-14T22:15:44.392441+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=name.herokuapp.com request_id=bfe49e51-d043-4673-9836-9ea8e978af84 fwd="217.159.161.125" dyno= connect= service= status=503 bytes= protocol=https
2022-02-14T22:13:56.000000+00:00 app[heroku-redis]: source=REDIS addon=redis-tapered-17025 sample#active-connections=1 sample#load-avg-1m=0.075 sample#load-avg-5m=0.08 sample#load-avg-15m=0.055 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15618680kB sample#memory-free=13418624kB sample#memory-cached=1140224kB sample#memory-redis=333656bytes sample#hit-rate=1 sample#evicted-keys=0
                Yes that was my mistake. The file is indeed called asgi.py. Edited on the post. @BrianDestura
– Erik
                Feb 14, 2022 at 22:37

I was able to fix the problem by changing position of os.environ.setdefault before imports. Apparently the order matters. I found a solution here

updated asgi.py
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_cs.settings')
django.setup()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import main.routing
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AuthMiddlewareStack(
        URLRouter(
            main.routing.websocket_urlpatterns
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.