From 7ab249bc8c8af45b078ff375cfd0f6b9f49d9024 Mon Sep 17 00:00:00 2001 From: Maksim Koryukov Date: Wed, 23 Aug 2017 07:27:47 +0500 Subject: [PATCH] Disable ".env not found" warning by default (#57) * Disable ".env not found" warning by default * Mention VERBOSE option in readme * Fix tests for `load_dotenv` with new verbose option --- README.rst | 3 +++ dotenv/main.py | 5 +++-- tests/test_core.py | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index e41aa9d0..7059b4f0 100644 --- a/README.rst +++ b/README.rst @@ -49,6 +49,9 @@ Add the following code to your ``settings.py`` dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) + # OR, the same with increased verbosity: + load_dotenv(dotenv_path, verbose=True) + Alternatively, you can use ``find_dotenv()`` method that will try to find a ``.env`` file by (a) guessing where to start using ``__file__`` or the working directory -- allowing this to work in non-file contexts such as IPython notebooks diff --git a/dotenv/main.py b/dotenv/main.py index de1eb6f0..45f99054 100644 --- a/dotenv/main.py +++ b/dotenv/main.py @@ -16,12 +16,13 @@ def decode_escaped(escaped): return __escape_decoder(escaped)[0] -def load_dotenv(dotenv_path): +def load_dotenv(dotenv_path, verbose=False): """ Read a .env file and load into os.environ. """ if not os.path.exists(dotenv_path): - warnings.warn("Not loading %s - it doesn't exist." % dotenv_path) + if verbose: + warnings.warn("Not loading %s - it doesn't exist." % dotenv_path) return None for k, v in dotenv_values(dotenv_path).items(): os.environ.setdefault(k, v) diff --git a/tests/test_core.py b/tests/test_core.py index 29f0f9a3..8e9d3f0a 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -10,7 +10,7 @@ def test_warns_if_file_does_not_exist(): with warnings.catch_warnings(record=True) as w: - load_dotenv('.does_not_exist') + load_dotenv('.does_not_exist', verbose=True) assert len(w) == 1 assert w[0].category is UserWarning