title: How to set "DJANGO_SETTINGS_MODULE" for a "config" module divided into common, local, production files
body: <p>I'm setting up a wsgi.py to deploy my django app in DigitalOcean, but I'm getting the following errors during deployment phase after successful build. From the error trace, I think there is a problem with <code>os.environ.setdefault(&quot;DJANGO_SETTINGS_MODULE&quot;, &quot;config&quot;)</code> this line inside wsgi.py.</p>
  <p>My config files are organized as a python module with common, local, and production settings. I'm not sure where and how to fix this problem, and I've been stuck here for a few days now. Would appreciate any input!</p>
  <h3>ERROR TRACE</h3>
  <pre><code>[2022-07-31 03:29:55] Traceback (most recent call last):
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 908, in _find_spec
  [2022-07-31 03:29:55] AttributeError: 'ConfigurationImporter' object has no attribute 'find_spec'
  [2022-07-31 03:29:55] 
  [2022-07-31 03:29:55] During handling of the above exception, another exception occurred:
  [2022-07-31 03:29:55] 
  [2022-07-31 03:29:55] Traceback (most recent call last):
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py&quot;, line 589, in spawn_worker
  [2022-07-31 03:29:55]     worker.init_process()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py&quot;, line 134, in init_process
  [2022-07-31 03:29:55]     self.load_wsgi()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py&quot;, line 146, in load_wsgi
  [2022-07-31 03:29:55]     self.wsgi = self.app.wsgi()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/app/base.py&quot;, line 67, in wsgi
  [2022-07-31 03:29:55]     self.callable = self.load()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py&quot;, line 58, in load
  [2022-07-31 03:29:55]     return self.load_wsgiapp()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py&quot;, line 48, in load_wsgiapp
  [2022-07-31 03:29:55]     return util.import_app(self.app_uri)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/gunicorn/util.py&quot;, line 359, in import_app
  [2022-07-31 03:29:55]     mod = importlib.import_module(module)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module
  [2022-07-31 03:29:55]     return _bootstrap._gcd_import(name[level:], package, level)
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 975, in _find_and_load_unlocked
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 671, in _load_unlocked
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 843, in exec_module
  [2022-07-31 03:29:55]   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed
  [2022-07-31 03:29:55]   File &quot;/code/scraft-server/wsgi.py&quot;, line 14, in &lt;module&gt;
  [2022-07-31 03:29:55]     from configurations.wsgi import get_wsgi_application  # noqa
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/configurations/wsgi.py&quot;, line 8, in &lt;module&gt;
  [2022-07-31 03:29:55]     application = get_wsgi_application()
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/django/core/wsgi.py&quot;, line 12, in get_wsgi_application
  [2022-07-31 03:29:55]     django.setup(set_prefix=False)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/django/__init__.py&quot;, line 19, in setup
  [2022-07-31 03:29:55]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 84, in __getattr__
  [2022-07-31 03:29:55]     self._setup(name)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 71, in _setup
  [2022-07-31 03:29:55]     self._wrapped = Settings(settings_module)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 179, in __init__
  [2022-07-31 03:29:55]     mod = importlib.import_module(self.SETTINGS_MODULE)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module
  [2022-07-31 03:29:55]     return _bootstrap._gcd_import(name[level:], package, level)
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/site-packages/configurations/importer.py&quot;, line 133, in find_module
  [2022-07-31 03:29:55]     imp.find_module(module, path))
  [2022-07-31 03:29:55]   File &quot;/usr/local/lib/python3.8/imp.py&quot;, line 296, in find_module
  [2022-07-31 03:29:55]     raise ImportError(_ERR_MSG.format(name), name=name)
  [2022-07-31 03:29:55] ImportError: No module named 'config'
  </code></pre>
  <h3>file tree</h3>
  <pre><code>monorepo/backend-server
  ├── Dockerfile
  ├── README.md
  ├── config
  │   ├── __init__.py
  │   ├── common.py
  │   ├── local.py
  │   └── production.py
  ├── docker-compose.yml
  ├── manage.py
  ├── requirements.txt
  ├── urls.py
  ├── wait_for_postgres.py
  └── wsgi.py
  </code></pre>
  <h3>wsgi.py</h3>
  <pre><code>import os
  
  os.environ.setdefault(&quot;DJANGO_SETTINGS_MODULE&quot;, &quot;config&quot;)
  os.environ.setdefault(&quot;DJANGO_CONFIGURATION&quot;, &quot;Production&quot;)
  
  # sys.path.append(&quot;/scraft-server&quot;)
  
  from configurations.wsgi import get_wsgi_application  # noqa
  
  application = get_wsgi_application()
  </code></pre>
  <h3>config.common.py</h3>
  <pre><code>import os
  from os.path import join
  from distutils.util import strtobool
  from dotenv import load_dotenv
  import datetime
  import dj_database_url
  from configurations import Configuration
  
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  
  load_dotenv()
  
  class Common(Configuration):
  
      # settings
  </code></pre>
  <h3>DockerFile</h3>
  <pre><code>FROM python:3.8
  ENV PYTHONUNBUFFERED 1
  
  # Allows docker to cache installed dependencies between builds
  COPY ./requirements.txt requirements.txt
  RUN pip install -r requirements.txt
  
  # Adds our application code to the image
  WORKDIR /code/scraft-server
  COPY scraft-server .  
  WORKDIR /code
  
  EXPOSE 8000
  
  # Run the production server
  CMD newrelic-admin run-program gunicorn --bind localhost:$PORT --access-logfile - scraft-server.wsgi:application
  </code></pre>
  <h1>EDIT</h1>
  <p>Editted DockerFile to the following:</p>
  <pre><code>FROM python:3.8
  ENV PYTHONUNBUFFERED 1
  ENV DJANGO_SETTINGS_MODULE backend-server.config.production
  ENV DJANGO_CONFIGURATION Production
  # ENV DJANGO_SETTINGS_MODULE backend-server.config.production
  
  # Allows docker to cache installed dependencies between builds
  COPY ./requirements.txt requirements.txt
  RUN pip install -r requirements.txt
  
  # Adds our application code to the image
  WORKDIR /code/backend-server
  COPY backend-server .  
  WORKDIR /code
  
  EXPOSE 8000
  
  # Run the production server
  CMD newrelic-admin run-program gunicorn --bind localhost:$PORT --access-logfile - backend-server.wsgi:application
  
  </code></pre>
  <h3>New Error Log</h3>
  <pre><code>-07-31 05:49:23]   File &quot;/code/scraft-server/wsgi.py&quot;, line 14, in &lt;module&gt;
  [2022-07-31 05:49:23]     from configurations.wsgi import get_wsgi_application  # noqa
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/site-packages/configurations/wsgi.py&quot;, line 8, in &lt;module&gt;
  [2022-07-31 05:49:23]     application = get_wsgi_application()
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/site-packages/django/core/wsgi.py&quot;, line 12, in get_wsgi_application
  [2022-07-31 05:49:23]     django.setup(set_prefix=False)
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/site-packages/django/__init__.py&quot;, line 24, in setup
  [2022-07-31 05:49:23]     apps.populate(settings.INSTALLED_APPS)
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/site-packages/django/apps/registry.py&quot;, line 91, in populate
  [2022-07-31 05:49:23]     app_config = AppConfig.create(entry)
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/site-packages/django/apps/config.py&quot;, line 223, in create
  [2022-07-31 05:49:23]     import_module(entry)
  [2022-07-31 05:49:23]   File &quot;/usr/local/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module
  [2022-07-31 05:49:23]     return _bootstrap._gcd_import(name[level:], package, level)
  [2022-07-31 05:49:23] ModuleNotFoundError: No module named 'users'
  </code></pre>