1

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!

Micromegas
  • 1,499
  • 2
  • 20
  • 49
  • I am pretty sure this has to do with the docker environment. Is there a way of running sphinx's ```make html``` with docker-compose? – Micromegas Nov 22 '19 at 12:23

2 Answers2

0

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

Micromegas
  • 1,499
  • 2
  • 20
  • 49
0

I had this same issue, related to the RTD build passing, but not rendering the whole thing properly.

Issue

My code had:

os.environ['MY_ENV_VAR']

The RTD build would pass, but not render any autodoc elements. The imports failed due to not being able to identify the key of ['MY_ENV_VAR'], raising a KeyError(key).

This error can be found by viewing the RAW output of build logs at your RTD account homepage under 'Builds'.

Solution

To resolve this, you can add the required environment keys ("POSTGRES_DB" for example), in your RTD account at Admin/Environment Variables. Here you can add the required keys, and in my experience, you can add nonsense values for the value. The RTD build will now fully pass as it can identify the Key(s) that you passed when calling os.environ[].

C. Peck
  • 3,641
  • 3
  • 19
  • 36
atomscale
  • 111
  • 1
  • 5