I want to make a package that works out of the box with reasonable default variables, like defining some servers, ports etc. so that the code works for an average user like he expects it without further configuration. But I want this environment variables to be overriden if a .env file exists in order to allow configuration for other environments. I read that python-dotenv load_values will use defaults if no .env file exists, but there is no example on pypi how that would set up ideally.
Default variables with python-dotenv
1.3k views Asked by FordPrefect At
3
There are 3 answers
0
On
python_dotenv is just convenient way to load variable to environment
You can add an option to override an already existing env variable:
load_dotenv(override=True)
The order how env are defined can be found here.
To load the default value, just use os.getenv
implementation
os.getenv(key, default = 'string value, or define None')
Although, you might not define value in order to make this work (define only key, without using =
)
Reading the comment of @chepner I think there might be a solution using merge. Didn't know about that feature yet.
This might be nice, because it allows for partial override and I don't need to go through all variables.
Comments are welcome.