Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] accessing variables using environment is not working when using include or preload. #1029

Open
gust-p opened this issue Nov 20, 2023 · 1 comment
Labels
Milestone

Comments

@gust-p
Copy link

gust-p commented Nov 20, 2023

Describe the bug
env_switcher or environments is not working

To Reproduce
Steps to reproduce the behavior:

  1. Having the following folder structure
Project structure
.
├── .env
├── config
│   ├── .secrets.toml
│   └── settings.toml
│   └── config.yaml
├── db.mysql
├── kpi.py
├── main.py
├── mypy.ini
├── poetry.lock
├── pyproject.toml
└── temporary.py


  1. Having the following config files:
Config files

./.env

APP_ENV="production"

and

/config/settings.toml

[development]
debug = true
database_url = 'sqlite:///dev.db'

[production]
debug = false
database_url = 'postgres:///prod.db'

and

/config/config.yaml

location: earth
  1. Having the following app code:
Code

./main.py

from dynaconf import Dynaconf, inspect_settings

envvar_prefix = "APP"
env_switcher = f"{envvar_prefix}_ENV"
default_env = "development"

settings = Dynaconf(
    settings_files=["config/.secrets.toml", "config/settings.toml"],
    includes=["config/config.yaml"],
    enviroments=True,
    load_dotenv=True,
    envvar_prefix=envvar_prefix,
    env_switcher=env_switcher,
)

inspect_settings(settings, print_report=True)

with settings.using_env("production"):
    print(f"Database URL: {settings.DATABASE_URL}")
    print(f"Secret Key: {settings.SECRET_KEY}")

...
  1. Executing under the following environment
Execution
# other commands and details?
# virtualenv activation? yes
python main.py or poetry run python main.py

Expected behavior
A clear and concise description of what you expected to happen.

Environment (please complete the following information):

  • OS: Ubuntu 22.04 LSTS
  • Dynaconf Version 3.2.4
  • Frameworks in use: None

Additional context
I have tried env_switcher=APP_ENV and running the app with APP_ENV=production python main.py or trying with settings.using_env()

@gust-p gust-p added the bug label Nov 20, 2023
@gust-p gust-p changed the title [bug] accessing variables using environment is not working in any way. [bug] accessing variables using environment is not working when using include or preload. Nov 21, 2023
@gust-p
Copy link
Author

gust-p commented Nov 21, 2023

Extra context:

When setting environments=True nothing is accessible, leading to AttributeError. And nothing on the config/config.yaml file is loaded.

header:
  env_filter: None
  key_filter: None
  new_first: 'True'
  history_limit: None
  include_internal: 'False'
current:
  ENVIROMENTS: true
  LOAD_DOTENV: true
  SECRET_KEY: supersecret
  DEVELOPMENT:
    debug: true
    database_url: sqlite:///dev.db
  PRODUCTION:
    debug: false
    database_url: postgres:///prod.db
  ENV: production
history:
- loader: env_global
  identifier: unique
  env: global
  merged: false
  value:
    ENV: production
- loader: toml
  identifier: /home/leno/Repositories/kpi_refactor/config/settings.toml
  env: default
  merged: false
  value:
    DEVELOPMENT:
      debug: true
      database_url: sqlite:///dev.db
    PRODUCTION:
      debug: false
      database_url: postgres:///prod.db
- loader: toml
  identifier: /home/leno/Repositories/kpi_refactor/config/.secrets.toml
  env: default
  merged: false
  value:
    SECRET_KEY: supersecret
- loader: set_method
  identifier: settings_module_method
  env: global
  merged: false
  value:
    SETTINGS_MODULE:
    - config/.secrets.toml
    - config/settings.toml
- loader: set_method
  identifier: envvars_first_load
  env: global
  merged: false
  value:
    ENV_FOR_DYNACONF: production
- loader: set_method
  identifier: init_kwargs
  env: global
  merged: false
  value:
    ENVIROMENTS: true
    LOAD_DOTENV: true
    INCLUDES_FOR_DYNACONF:
    - config/config.yaml
    ENV_SWITCHER_FOR_DYNACONF: APP_ENV
    ENVVAR_PREFIX_FOR_DYNACONF: APP
    SETTINGS_FILE_FOR_DYNACONF:
    - config/.secrets.toml
    - config/settings.toml

With environments=False and preload=["config/config.yaml"]:

header:
  env_filter: None
  key_filter: None
  new_first: 'True'
  history_limit: None
  include_internal: 'False'
current:
  LOAD_DOTENV: true
  ENVIROMENTS: false
  LOCATION: earth
  SECRET_KEY: supersecret
  DEVELOPMENT:
    debug: true
    database_url: sqlite:///dev.db
  PRODUCTION:
    debug: false
    database_url: postgres:///prod.db
  ENV: production
history:
- loader: env_global
  identifier: unique
  env: global
  merged: false
  value:
    ENV: production
- loader: toml
  identifier: /home/leno/Repositories/kpi_refactor/config/settings.toml
  env: default
  merged: false
  value:
    DEVELOPMENT:
      debug: true
      database_url: sqlite:///dev.db
    PRODUCTION:
      debug: false
      database_url: postgres:///prod.db
- loader: toml
  identifier: /home/leno/Repositories/kpi_refactor/config/.secrets.toml
  env: default
  merged: false
  value:
    SECRET_KEY: supersecret
- loader: set_method
  identifier: settings_module_method
  env: global
  merged: true
  value:
    SETTINGS_MODULE:
    - config/.secrets.toml
    - config/settings.toml
- loader: yaml
  identifier: /home/leno/Repositories/kpi_refactor/config/config.yaml
  env: default
  merged: false
  value:
    LOCATION: earth
- loader: set_method
  identifier: envvars_first_load
  env: global
  merged: false
  value:
    ENV_FOR_DYNACONF: production
- loader: set_method
  identifier: init_kwargs
  env: global
  merged: false
  value:
    LOAD_DOTENV: true
    ENVIROMENTS: false
    SETTINGS_FILE_FOR_DYNACONF:
    - config/.secrets.toml
    - config/settings.toml
    ENVVAR_PREFIX_FOR_DYNACONF: APP
    PRELOAD_FOR_DYNACONF:
    - config/config.yaml
    MERGE_ENABLED_FOR_DYNACONF: true
    ENV_SWITCHER_FOR_DYNACONF: APP_ENV

@pedro-psb pedro-psb added this to the 3.3.x milestone Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants