HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.79k stars 192 forks source link

profiling through environment variables #164

Closed bigusef closed 9 months ago

bigusef commented 11 months ago

I have an application and using python-decouple to read environment variable on local environment or production environment. I need to be able to have multiple profiles on local machine. so have file contains production credential and another file for staging credential. also still have a third file contains common credential and values not changed through different environments. any change I can get this by python-decouple

almdudler777 commented 11 months ago

I don't think this is possible atm. python-decouple will load settings.ini (with one section named "settings") or an .env file if .ini not found.

While you can instantiate the Config class with a different search path i think the filenames are fixed.

If you wanted to retain the casting and defaulting functionality of python-decouple you could try to also use python-dotenv and its read_env function to read in multiple files with Environment Variables. These should be added to os.environ (python standard library) which decouple also uses. This way you could read an arbitrary amount of files named however you like.

Have not tested, i only used django-dotenv which i replaced with decouple because i started implementing all the casting functionality myself ... and i only use one .env file. But i think the idea should work...

EDIT: you could also organize your files into folders:

- /settings
-- /production
--- /config1
---- .env
--- /config2
---- .env
-- /local
--- /config1
---- .env
--- /config2
---- .env

and so on... you would then do this in python

from decouple import AutoConfig

prod_config1 = AutoConfig(search_path="settings/production/config1/")  # will load .env from this directory
prod_config2 = AutoConfig(search_path="settings/production/config2/")
# ... and so on
var1 = prod_config1('var1', default=True, cast=bool)

Again untested :) hope this helps.