ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

Each Answer to this Q is separated by one/two green lines.

I was trying to configure my Django project to deploy to Heroku. I am getting the following error and I don’t really know how to fix it.

Here is the full traceback and error:

22:46:15 web.1  | Traceback (most recent call last):
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/", line 495, in spawn_worker
22:46:15 web.1  |     worker.init_process()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/workers/", line 106, in init_process
22:46:15 web.1  |     self.wsgi =
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/", line 114, in wsgi
22:46:15 web.1  |     self.callable = self.load()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/", line 62, in load
22:46:15 web.1  |     return self.load_wsgiapp()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/", line 49, in load_wsgiapp
22:46:15 web.1  |     return util.import_app(self.app_uri)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/", line 354, in import_app
22:46:15 web.1  |     __import__(module)
22:46:15 web.1  |   File "/Users/nir/nirla/nirla/", line 12, in <module>
22:46:15 web.1  |     from dj_static import Cling
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/", line 7, in <module>
22:46:15 web.1  |     from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/", line 8, in <module>
22:46:15 web.1  |     from django.contrib.staticfiles.views import serve
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/", line 13, in <module>
22:46:15 web.1  |     from django.views import static
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/views/", line 96, in <module>
22:46:15 web.1  |     template_translatable = ugettext_noop("Index of %(directory)s")
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/", line 65, in gettext_noop
22:46:15 web.1  |     return _trans.gettext_noop(message)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/", line 52, in __getattr__
22:46:15 web.1  |     if settings.USE_I18N:
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/", line 54, in __getattr__
22:46:15 web.1  |     self._setup(name)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/", line 47, in _setup
22:46:15 web.1  |     % (desc, ENVIRONMENT_VARIABLE))
22:46:15 web.1  | ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Here is my file:

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings") #nirla is the name of the project

application = Cling(get_wsgi_application())

And in case it was relevant, my file:

import os
import sys

    if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings")
        from import execute_from_command_line

Can anyone seem to understand the issue here? And if so, can you be so kind as to explain what is wrong?

I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found:

export DJANGO_SETTINGS_MODULE=mysite.settings

Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this:

heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name> 

In my specific case I ran these two and everything worked out:

export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal


I would also like to point out that you have to re-do this every time you close or restart your virtual environment. Instead, you should automate the process by going to venv/bin/activate and adding the line: set DJANGO_SETTINGS_MODULE=mysite.settings to the bottom of the code. From now on every time you activate the virtual environment, you will be using that app’s settings.

From The Definitive Guide to Django: Web Development Done Right:

If you’ve used Python before, you may be wondering why we’re running python shell instead of just python. Both commands will start the interactive interpreter, but the shell command has one key difference: before starting the interpreter, it tells Django which settings file to use.

Use Case: Many parts of Django, including the template system, rely on your settings, and you won’t be able to use them unless the framework knows which settings to use.

If you’re curious, here’s how it works behind the scenes. Django looks for an environment variable called DJANGO_SETTINGS_MODULE, which should be set to the import path of your For example, DJANGO_SETTINGS_MODULE might be set to 'mysite.settings', assuming mysite is on your Python path.

When you run python shell, the command takes care of setting DJANGO_SETTINGS_MODULE for you.**

Django needs your application-specific settings. Since it is already inside your, just use that. The faster, but perhaps temporary, solution is:

python shell

In my case it was the use of the call_command module that posed a problem.
I added set DJANGO_SETTINGS_MODULE=mysite.settings but it didn’t work.

I finally found it:

add these lines at the top of the script, and the order matters.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

import django

from import call_command

In my case, it was a Python path issue.

  1. First set your PYTHONPATH
  3. then run django-admin shell command (django-admin dbshell)
(venv) [email protected]:~/project_path$ export PYTHONPATH=/home/shakeel/project_path
(venv) [email protected]:~/project_path$ export DJANGO_SETTINGS_MODULE=my_project.settings
(venv) [email protected]:~/project_path$ django-admin dbshell
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.

otherwise python shell works like charm.

If you are here due to error while trying to run daphne on server then here is the answer for you .

Change you file like this.

import os
from django.conf.urls import url

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

from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
django_asgi_app = get_asgi_application()

from channels.auth import AuthMiddlewareStack   
import api_backend.routing

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    'websocket': AuthMiddlewareStack(

Create a .env file that will hold your credentials at the root of your project and leave it out of versioning:

$ echo ".env" >> .gitignore

In the .env file, add the variables (adapt them according to your installation):

$ echo "DJANGO_SETTINGS_MODULE=myproject.settings.production"> .env
#50 caracter random key
$ echo "SECRET_KEY='####'">> .env

To use them, put this on top of your settings file:

import os

env = os.environ.copy()

Publish it to Heroku using this gem:

$ heroku plugins:install git://
$ heroku config:push

This way you avoid to change virtualenv files.

*Based on this tutorial

If you are using the local server, run Django shell using python shell. It will take you to the Django python environment and you are good to go.

I found this error when I set in my list_display = ('name',) I changed it to list_display = ('name','date') And it worked.

I have used pipenv to manage virtual environment for django project.


1. pipenv shell
2. python shell

I had a wrong way of import.

First I imported models of django without (before I declare that the file use django)

import pika, json, os, django
from products.models import Product

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")

But then I changed the import as below and it works (basically declaring file uses django and then importing models and other apps)

import pika, json, os, django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")

from products.models import Product

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.