-
Notifications
You must be signed in to change notification settings - Fork 487
Description
In some scenarios (say if you want clear deterministic behaviour for a production app) it is desirable to immediately fail with an obvious error if the specified env file is absent, instead of starting the application in a potentially problematic way due to missing configuration. Clear behaviour, explicit requirements, and obvious failures are important to avoid fragile situations, such as an app working in production by accidentally getting the right config from an unexpected place. Maybe later you clean up other files that you think aren't needed, but then the app breaks because it was not actually getting the config from where you thought it was.
There have been several issues discussing this (both in dotenv and downstream environs):
- no error or failure is reported when specified .env file does not exist sloria/environs#199
- Add argument
raise_error_if_not_foundfor read_env sloria/environs#165 - dotenv_load with bad file path doesn't error #164
- Update load_dotenv to return False if no dotenv source is loaded #263
- load_dotenv() returns True even if .env file is not found #321
To provide a solution for these types of problems, I propose a new option, something like "requireFile", for load_dotenv. It can be false by default to preserve current behaviour. @theskumar
If true, it would enable a strict file existence requirement for load_dotenv , so that it fails if no env file is found, no matter how/where (whether it is a named one specified by dotenv_path, or the default ".env" or found by recursing directories ).
And the behaviour of how/where env files are found would remain unchanged. Would that be a reasonable way to address this?