dynaconf / dynaconf

Configuration Management for Python ⚙
https://dynaconf.com
MIT License
3.75k stars 290 forks source link

RFC: Add a way to disable envvar loading and default prefix #782

Closed rochacbruno closed 3 months ago

rochacbruno commented 2 years ago

It's a great library and I am enjoying it. However I would like to make a small feature request if it is not already there. I want a way to disable environmental variable overwriting the variables declared in config files.
I removed the below line from the Dynaconf() class parameter , still it's the same.
envvar_prefix = "DYNACONF"

I ever created a new "envvarprefix" however , it seems the env prefix "DYNACONF.." is a default and always being checked even if you have a different envvar_prefix set.

Could you please consider below as the feature requests if there is no answer: 1) Is there a way to completely disable the environmental variable overwriting the existing ? 2) Is there a way to completely disable the check up of environmental variable "DYNACONF_" when different envvar_prefix is like below ? envvar_prefix = "MYAPP"

Originally posted by @swagatsourav in https://github.com/dynaconf/dynaconf/discussions/781

b4stien commented 1 year ago

I removed the below line from the Dynaconf() class parameter , still it's the same. envvar_prefix = "DYNACONF"

The doc says DYNACONF it's the default value for envvar_prefix anyway (cf this paragraph), as expected, removing the explicit set to the default has no effect.

Your 2. sounds like a bug.

For 1., deep down inside the paragraph for external loader there is a hint towards LOADERS_FOR_DYNACONF to do what you want. If you pass an empty list you might be able to disable the default environment variable lookup.

(Take all this with a grain of salt, I've just read the doc and never used the software)

swagatsourav commented 1 year ago

I removed the below line from the Dynaconf() class parameter , still it's the same. envvar_prefix = "DYNACONF"

The doc says DYNACONF it's the default value for envvar_prefix anyway (cf this paragraph), as expected, removing the explicit set to the default has no effect.

Your 2. sounds like a bug.

For 1., deep down inside the paragraph for external loader there is a hint towards LOADERS_FOR_DYNACONF to do what you want. If you pass an empty list you might be able to disable the default environment variable lookup.

(Take all this with a grain of salt, I've just read the doc and never used the software)

@b4stien , Thanks for the suggestion on point no 1. It's working.

pedro-psb commented 1 year ago
  1. Is there a way to completely disable the check up of environmental variable "DYNACONF_" when different envvar_prefix is like below?

There is also a workaround. This example from the docs achieves that with filtering strategies.

from dynaconf import Dynaconf
from dynaconf.strategies.filtering import PrefixFilter

settings = Dynaconf(
    settings_file="settings.toml",
    environments=False,
    filter_strategy=PrefixFilter("prefix")
)

After that, env var that have a different prefix than that specified in the PrefixFilter won't get loaded.

Still, I think this should be the default too.

techdragon commented 1 year ago

I think solving this could also be a way to solve an issue I've had occasionally.

It would be good to be able to filter the loading process so that only a specific set of desired variables are parsed in, without the need to create a settings file, or modify them with a prefix... A way to say "only these variables please" from a code + environment variables pathway where there's no additional files getting created. While I could go around injecting settings files into my Kubernetes pods, I prefer to simply use environment variables wherever possible and in the case of library code that might get reused, having as narrow a scope as possible is a good thing.

If there was a way to filter the loaded environment variables it would enable the ability to set that filter to an "empty list" and thus disable loading anything via the environment variables.

rochacbruno commented 1 year ago

@techdragon you can pass https://www.dynaconf.com/envvars/#environment-variables-filtering ignore_unknown_envvars=True and then it will load only variables that are defined.

You can define a variable via Validators or to the instance initialization

app.py

settings = Dynaconf(
    ignore_unknown_envvars=True,
    thing="default value",
    envvar_prefix=False
)
export THING="other value"
export IGNORED=0
dynaconf -i app.settings list
THING<str>:"other value"
mitches-got-glitches commented 7 months ago

I've just come across this too thinking it seemed like a bug, certainly unexpected.

Reproduced this with Python 3.11.4 and dynaconf 3.2.4:

import os

from dynaconf import Dynaconf
from dynaconf.strategies.filtering import PrefixFilter

os.environ["APP_CLASSIFIER"] = "logistic_regression"
os.environ["DYNACONF_DEBUG"] = "true"
os.environ["DYNACONF_BLADE"] = "runner"
# This will take precedence over DYNACONF_DEBUG if uncommented.
# os.environ["APP_DEBUG"] = "false"

settings = Dynaconf(envvar_prefix="APP")
# Adding the filter too only returns those prefixed with "APP_".
# But seems like unnecessary repetition - should be the default?
# settings = Dynaconf(envvar_prefix="APP", filter_strategy=PrefixFilter("APP"))

if __name__ == "__main__":
    print(settings.classifier)
    >>> "logistic_regression"
    # These shouldn't be available...
    print(settings.debug)
    >>> True
    # If APP_DEBUG uncommented:
    # >>> False
    print(settings.blade)
    >>> "runner"

I think I would upvote making it the default to exclude any env vars prefixed with "DYNACONF" when another prefix is specified to avoid polluting the settings space unintentionally.

rochacbruno commented 3 months ago

1156 to take care of it