Skip to content

[bug] LOAD_DOTENV_FOR_DYNACONF environment variable does not work #720

@tokr94

Description

@tokr94

Describe the bug
When setting LOAD_DOTENV_FOR_DYNACONF prior to load settings, variables from .env file are ignored unless I specify load_dotenv=True in class constructor.

To Reproduce
Steps to reproduce the behavior:

  1. Having the following folder structure
Project structure
.
├── .env
├── config
│   ├── settings-staging.yaml
│   └── settings.yaml
└── src
    └── app
        └── app.py
  1. Having the following config files:
Config files

config/settings.yaml

default:
  foo:
    bar:

and

config/settings-staging.yaml

staging:
  foo:
    bar: baz

and

.env

MERGE_ENABLED_FOR_DYNACONF=true
ENV_FOR_DYNACONF=staging
ROOT_PATH_FOR_DYNACONF=config
ENVIRONMENTS_FOR_DYNACONF=true
  1. Having the following app code:
Code

src/app/app.py

import os
from pathlib import Path
from dynaconf import Dynaconf

env = {key: val for key, val in os.environ.items() if 'DYNACONF' in key}
assert env == {}, f'Dynaconf environment not empty {env}'

os.environ['LOAD_DOTENV_FOR_DYNACONF'] = 'true'

settings_files = [
    Path.cwd() / 'config' / 'settings.yaml',
    Path.cwd() / 'config' / 'settings-staging.yaml']

assert all(p.exists() for p in settings_files), 'Some of the given config files do not exist'

settings = Dynaconf(
    settings_module=settings_files,
    # load_dotenv=True
    )

assert settings.FOO.get('bar') == 'baz', 'Missing key FOO=bar in settings'
  1. Executing under the following environment
Execution
# conda 4.11.0
$ conda create -p venv python=3.7.2
$ venv/bin/python /path/src/app.py

Expected behavior
As described previously I expected that Dynaconf would load environment from .env file. More precisely, the exact same behaviour when setting load_dotenv=True in class constructor. Instead, Dynaconf raises an AttributeError: 'Settings' object has no attribute 'FOO'.

Environment (please complete the following information):

  • System Version: macOS 12.2.1 (21D62)
  • Kernel Version: Darwin 21.3.0
  • Dynaconf Version 3.1.7

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions