My website works fine on localhost, but not on Heroku (it just crashes)

I read through the logs and found this confusing error.

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This is a bit weird because this is what my wsgy.py contains:

import os

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PersonalWebsite.settings')

I looked it up on DuckDuckGo already, and tried answers/solutions from StackOverflow and some random websites, but it still doesn’t work ;-;

It’s really weird because this works fine when I open it on localhost, but not on my Heroku app website thing.

Complete logs:

2020-08-02T07:21:24.000000+00:00 app[api]: Build started by user (email removed)
2020-08-02T07:22:17.025507+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-02T07:22:16.813627+00:00 app[api]: Deploy 9349094c by user (email removed)
2020-08-02T07:22:16.813627+00:00 app[api]: Release v72 created by user (email removed)
2020-08-02T07:22:24.078201+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-08-02T07:22:24.078715+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [4] [INFO] Listening at: http://0.0.0.0:50319 (4)
2020-08-02T07:22:24.078805+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [4] [INFO] Using worker: sync
2020-08-02T07:22:24.082725+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [9] [INFO] Booting worker with pid: 9
2020-08-02T07:22:24.119278+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [10] [INFO] Booting worker with pid: 10
2020-08-02T07:22:24.777340+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [10] [ERROR] Exception in worker process
2020-08-02T07:22:24.777363+00:00 app[web.1]: Traceback (most recent call last):
2020-08-02T07:22:24.777365+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-08-02T07:22:24.777366+00:00 app[web.1]: worker.init_process()
2020-08-02T07:22:24.777366+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-08-02T07:22:24.777366+00:00 app[web.1]: self.load_wsgi()
2020-08-02T07:22:24.777367+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-08-02T07:22:24.777367+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-08-02T07:22:24.777368+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-08-02T07:22:24.777368+00:00 app[web.1]: self.callable = self.load()
2020-08-02T07:22:24.777369+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-08-02T07:22:24.777369+00:00 app[web.1]: return self.load_wsgiapp()
2020-08-02T07:22:24.777369+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-08-02T07:22:24.777370+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-08-02T07:22:24.777370+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2020-08-02T07:22:24.777371+00:00 app[web.1]: mod = importlib.import_module(module)
2020-08-02T07:22:24.777371+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-08-02T07:22:24.777372+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-08-02T07:22:24.777373+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-08-02T07:22:24.777373+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-08-02T07:22:24.777373+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-08-02T07:22:24.777374+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-08-02T07:22:24.777374+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-08-02T07:22:24.777374+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-08-02T07:22:24.777375+00:00 app[web.1]: File "/app/PersonalWebsite/wsgi.py", line 14, in <module>
2020-08-02T07:22:24.777375+00:00 app[web.1]: application = get_wsgi_application()
2020-08-02T07:22:24.777376+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-08-02T07:22:24.777376+00:00 app[web.1]: django.setup(set_prefix=False)
2020-08-02T07:22:24.777376+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2020-08-02T07:22:24.777377+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2020-08-02T07:22:24.777377+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
2020-08-02T07:22:24.777377+00:00 app[web.1]: self._setup(name)
2020-08-02T07:22:24.777378+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 57, in _setup
2020-08-02T07:22:24.777378+00:00 app[web.1]: raise ImproperlyConfigured(
2020-08-02T07:22:24.777388+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2020-08-02T07:22:24.777555+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-08-02T07:22:24.783284+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [9] [ERROR] Exception in worker process
2020-08-02T07:22:24.783285+00:00 app[web.1]: Traceback (most recent call last):
2020-08-02T07:22:24.783286+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-08-02T07:22:24.783286+00:00 app[web.1]: worker.init_process()
2020-08-02T07:22:24.783287+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-08-02T07:22:24.783287+00:00 app[web.1]: self.load_wsgi()
2020-08-02T07:22:24.783288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-08-02T07:22:24.783288+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-08-02T07:22:24.783288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-08-02T07:22:24.783290+00:00 app[web.1]: self.callable = self.load()
2020-08-02T07:22:24.783290+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-08-02T07:22:24.783290+00:00 app[web.1]: return self.load_wsgiapp()
2020-08-02T07:22:24.783291+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-08-02T07:22:24.783291+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-08-02T07:22:24.783292+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2020-08-02T07:22:24.783292+00:00 app[web.1]: mod = importlib.import_module(module)
2020-08-02T07:22:24.783292+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-08-02T07:22:24.783293+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-08-02T07:22:24.783293+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-08-02T07:22:24.783294+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-08-02T07:22:24.783294+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-08-02T07:22:24.783294+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-08-02T07:22:24.783295+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-08-02T07:22:24.783295+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-08-02T07:22:24.783296+00:00 app[web.1]: File "/app/PersonalWebsite/wsgi.py", line 14, in <module>
2020-08-02T07:22:24.783296+00:00 app[web.1]: application = get_wsgi_application()
2020-08-02T07:22:24.783297+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-08-02T07:22:24.783297+00:00 app[web.1]: django.setup(set_prefix=False)
2020-08-02T07:22:24.783297+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2020-08-02T07:22:24.783298+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2020-08-02T07:22:24.783298+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
2020-08-02T07:22:24.783298+00:00 app[web.1]: self._setup(name)
2020-08-02T07:22:24.783299+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 57, in _setup
2020-08-02T07:22:24.783299+00:00 app[web.1]: raise ImproperlyConfigured(
2020-08-02T07:22:24.783300+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2020-08-02T07:22:24.783604+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [9] [INFO] Worker exiting (pid: 9)
2020-08-02T07:22:24.945396+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [4] [INFO] Shutting down: Master
2020-08-02T07:22:24.945572+00:00 app[web.1]: [2020-08-02 07:22:24 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-08-02T07:22:25.031009+00:00 heroku[web.1]: Process exited with status 3
2020-08-02T07:22:25.089713+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-02T07:22:26.000000+00:00 app[api]: Build succeeded

It works fine on your localhost because django is configured to automatically identify your wsgi or asgi.py file when runserver is executed.

When deploying to a service like Heroku, AWS, or Digital Ocean, you need to tell the service where that file is so it can constantly run your software. Do you know what service Heroku is using in the background? Nginx, maybe?

1 Like

It does look as if you are missing the Procfile for your Heroku deployment. I recommend you look at this doc: https://devcenter.heroku.com/articles/django-app-configuration

1 Like

Turns out I accidentally screwed up my wsgi file. I made a new project and compared it with my current project, and then I found a solution!
I changed my code from this:

application = get_wsgi_application()

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PersonalWebsite.settings')

to this:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PersonalWebsite.settings')

application = get_wsgi_application()

That’ll do it! Now, if this same issue occurs in the future, you know the resolution :slight_smile:

1 Like