pythondjangodjango-settingspython-decouple

python-decouple | The SECRET_KEY setting must not be empty


The project was created using Django 3.1.1. It was recently updated it to Django 3.1.4 and it worked fine. Afterwards, python-decouple 3.3 was installed and it also worked fine. We deleted our testing database (on purpose) to see if everything was fine and this ocurred. When running python manage.py makemigrations the following error apprears

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
   utility.execute()
  File "P:\crm\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv 
    self.execute(*args, **cmd_options)
  File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 368, in execute
    self.check()
  File "P:\crm\env\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "P:\crm\env\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "P:\crm\env\lib\site-packages\django\core\checks\templates.py", line 22, in 
    check_setting_app_dirs_loaders
    for conf in settings.TEMPLATES
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
    self._setup(name)
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\rodri\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "P:\crm\app\config\settings.py", line 13, in <module>
     from core.user.models import User as u
  File "P:\crm\app\core\user\models.py", line 1, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser
  File "P:\crm\env\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "P:\crm\env\lib\site-packages\django\db\models\base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "P:\crm\env\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
    self._setup(name)
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "P:\crm\env\lib\site-packages\django\conf\__init__.py", line 196, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

My project structure is as follows:

project
|
|- config
|    |- .env
|    |- settings.py
|- core
     |- app1
     |- app2

This is my settings.py

import config.db as db
from core.user.models import User # Custom user model
from decouple import config
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', cast=bool)
...

This is the .env file structure

SECRET_KEY=abcdefghikjlmnopqrstuvwxyz
DEBUG=True
...

I've tried many alternatives that are suggested here but none of them has worked.


Solution

  • The problem is you cannot import models before your settings are setup. So when you import the models at the top of the settings file, you get the error because the settings object hasn't been initialized, and therefore doesn't have the SECRET_KEY field set.

    Ideally you wouldn't need to import models in your settings at all. But if you have to have them in there, you MAY be able to simply move them later than the SECRET_KEY, but...it's probably still going to cause problems. I'd try to refactor so you don't need them in your settings file.