djangodjango-settingsdjango-3.2

Django - separated settings by environment. Not finding variable from base settings


I've split my django environment as per this post.

In settings/base.py I have BASE_DIR specified:

BASE_DIR = Path(__file__).resolve().parent.parent.parent

In settings/__init__.py I have:

from .base import *
env = os.environ['ENV']
...
if env == 'test':
  from .test import *
...

In settings/test.py I have DATA_VOLUME_BASE_DIR = BASE_DIR / 'scanned_images'.

I expect that Django loads the settings module, imports settings/base.py, checks the environment ('test'), imports settings/test.py (which works) and the variable is available (which isn't). My stacktrace:

sid_test_web | Traceback (most recent call last):
sid_test_web |   File "/code/manage.py", line 22, in <module>
sid_test_web |     main()
sid_test_web |   File "/code/manage.py", line 18, in main
sid_test_web |     execute_from_command_line(sys.argv)
sid_test_web |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_
sid_test_web |     utility.execute()
sid_test_web |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 363, in execute
sid_test_web |     settings.INSTALLED_APPS
sid_test_web |   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in __getattr__
sid_test_web |     self._setup(name)
sid_test_web |   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 69, in _setup
sid_test_web |     self._wrapped = Settings(settings_module)
sid_test_web |   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 170, in __init__
sid_test_web |     mod = importlib.import_module(self.SETTINGS_MODULE)
sid_test_web |   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
sid_test_web |     return _bootstrap._gcd_import(name[level:], package, level)
sid_test_web |   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
sid_test_web |   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
sid_test_web |   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
sid_test_web |   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
sid_test_web |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
sid_test_web |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
sid_test_web |   File "/code/my_project/settings/__init__.py", line 9, in <module>
sid_test_web |     from .test import *
sid_test_web |   File "/code/my_project/settings/test.py", line 39, in <module>
sid_test_web |     DATA_VOLUME_BASE_DIR = BASE_DIR / 'scanned_images'
sid_test_web | NameError: name 'BASE_DIR' is not defined

How do I make BASE_DIR variable available?


Solution

  • You have to import the .base setting in your test settings to make them available in test.py

    from .base import *
    # ... your other test settings
    

    The second thing is to choose which settings you like in the __init__.py.

    env = os.environ['ENV']
    if env == 'test':
        from .test import *
    else:
        from .base import *