Search code examples
djangodockerenvironment-variablespython-sphinxcookiecutter-django

Django sphinx documentation does not read environment variables in settings file


I want to document my cookiecutter django project with sphinx. The problem is that when running make html sphinx gives me problems reading the config file. It says django.core.exceptions.ImproperlyConfigured: Set the USE_DOCKER environment variable

When not calling django.setup() it also throws me an error with my envs: django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB environment variable When I hardcode them, the error goes on to complain about the next environment variable. I can't hardcode them all into the config file, that is not an option.

My environment variables are properly configured. When I print them out running my localhost they are there. It seems that somehow sphinx cannot process them. I am also using docker, so maybe that could interfere but I don't know. Here are parts of my sphinx config:

sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.local'

Here are parts of my local settings:


# ------------------------------------------------------------------------------
# https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration
INSTALLED_APPS += ['django_extensions']  # noqa F405


# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    # 'default': env.db('DATABASE_URL'),  # This was the default value, but modification below seemed necessary
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env("POSTGRES_DB"),
        'USER': env("POSTGRES_USER"),
        'PASSWORD': env("POSTGRES_PASSWORD"),
        'HOST': env("POSTGRES_HOST"),
        'PORT': env("POSTGRES_PORT"),
     }
}

Grateful for any kind of help. Thanks in advance!


Solution

  • So it turned out that apparantly some applications can't use environ to read from settings from file. Another problem was that docker alpine image doesn't come with make pre installed.

    I got it to work by installing make in my docker image (apk add make) and building the doc while spinning up the container with docker-compose -f local.yml run django make -C ./docs html. Thanks goes out to uzi0espil for leading me there.

    For more information see:

    https://github.com/pydanny/cookiecutter-django/issues/1747 https://github.com/cookiecutter/cookiecutter/issues/1251