question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add the ability to load only a specified list of keys from envvars

See original GitHub issue

Describe the bug Migrating from other configuration systems I’d need to configure envvar_prefix to empty string ("") so I can load any environment variable I used before. But it doesn’t seem to work with dynaconf.

To Reproduce Steps to reproduce the behavior:

  1. Create a basic dynaconf configuration

In config.py:

from dynaconf import Dynaconf

settings = Dynaconf(
    # This is the important line.
    envvar_prefix="",
)

print(settings.FOO)
  1. Now create some settings file.

In settings.py:

FOO = 'bar'
  1. Finally try running.
export SETTINGS_FILE_FOR_DYNACONF=settings
FOO='ham' python config.py

You will get:

bar

Expected behavior

The print output states:

ham

This means the value from environment variable has overridden the default value from settings.py module

Environment (please complete the following information):

  • OS: Ubuntu Linux 20.04
  • Python version: 3.8.5
  • Dynaconf version: 3.1.2
  • Frameworks in use: none.

Additional context Interestingly running in a “hacky” way changes the variable value:

_FOO='ham' python config.py

This is not what I’d expect though. The point is to modify the bare FOO variable.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
rochacbrunocommented, Nov 18, 2020

test case can be added here: https://github.com/rochacbruno/dynaconf/blob/master/tests/test_env_loader.py

and optionally a functional example in the example folder like this one https://github.com/rochacbruno/dynaconf/tree/master/example/envvar_prefix

1reaction
rochacbrunocommented, Nov 18, 2020

First you need to add the flag, any new variable on default_settings.py named in the pattern IGNORE_UNKNOWN_ENVVARS_FOR_DYNACONF will be a flag in `Dynaconf(…, ignore_unknown_envvars=True)

Here:

https://github.com/rochacbruno/dynaconf/blob/master/dynaconf/default_settings.py#L73-L222

Then you can access the variable using obj.get('IGNORE_UNKNOWN_ENVVARS_FOR_DYNACONF') here where the envvars are loaded.

https://github.com/rochacbruno/dynaconf/blob/master/dynaconf/loaders/env_loader.py#L24-L63

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add the ability to load only a specified list of keys from envvars
I noticed, using envvar_prefix=False causes Dynaconf to load absolutely all environment variables into the result LazySettings , e.g.: PATH / ...
Read more >
Environment variables to configure the AWS CLI
Environment variables provide another way to specify configuration options and credentials, and can be useful for scripting or temporarily setting a named ...
Read more >
Using Environment Variables | Cloud Functions Documentation
Runtime environment variables are key/value pairs deployed alongside a function. These variables are scoped to the function and are not visible to other ......
Read more >
How to Use Environment Variables in VanillaJS
In this article, you will learn about environment variables in Vanilla JavaScript. You'll also learn how to serve API keys to your ...
Read more >
Use environment variables in solutions - Power Apps
Environment variables store the parameter keys and values, ... The same is true when selecting SharePoint lists for a given site.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found