HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.83k stars 196 forks source link

Updating variables stored in .env files #114

Closed assman closed 3 years ago

assman commented 3 years ago

Hi I would like to clear a doubt. What happens if I update a variable value in the .env file? Do I have to reload config or redeploy in order for the app to read the new value?

henriquebastos commented 3 years ago

Yes, you need to reload config.

Em dom., 30 de mai. de 2021 às 06:35, Varun Parmar @.***> escreveu:

Hi I would like to clear a doubt. What happens if I update a variable value in the .env file? Do I have to reload config or redeploy in order for the app to read the new value?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/henriquebastos/python-decouple/issues/114, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAC6IRXBGD34UGMB6XKJYTTQIBG5ANCNFSM45ZFG4BA .

--

Henrique Bastos http://henriquebastos.net/ Muito além da programação! https://henriquebastos.net/produtos/

Instagram https://instagram.com/henriquebastosnet, Youtube https://www.youtube.com/henriquebastosnet, Facebook https://www.facebook.com/henriquebastos +55 21 99618-6180 https://hbn.link/whatsapp-duvida

path9263 commented 2 years ago

Yes, you need to reload config.

How do you reload the config? Thanks!

Edit: seems like this does it:

from decouple import config, AutoConfig
config = AutoConfig(search_path=BASE_DIR)

Where BASE_DIR is the directory containing your .env file.

ranelpadon commented 1 year ago

@path9263 Thanks for the tip, checked the source code and it's indeed the case. AutoConfig will force reload/re-open the file. I think this bit is useful to include in the README. Cheers

ranelpadon commented 1 year ago

Turned out that AutoConfig doesn't auto-update as well.

Here's my current code/import:

from decouple import config

We use some AWS API, turned out it sets some environment variables during runtime. So, when I update the .env file while the program is running, the updated value will not be fetched by config(): it will fetch the os.environ version instead.

Use case: at some point during program runtime, the program will pause for a chance to update the .env file. After updating the file, the program will continue, and it should get the updated values. Finally, made it work by deleting the os.environ values first, then reloading the imported module:

envvars = [
    'AWS_ACCESS_KEY_ID',
    'AWS_SECRET_ACCESS_KEY',
    'AWS_SESSION_TOKEN',
]

# Remove the old/priority values.
# This is a critical step.
for envvar in envvars:
    if os.getenv(envvar):
        del os.environ[envvar]

# Reload the imported module to clear its module cache.
import importlib
importlib.reload(decouple)
from decouple import config

# `config()` will now fetch the updated values from `.env`.
for envvar in envvars:
    os.environ[envvar] = config(envvar, default='')