Skip to content

Commit

Permalink
Disable ".env not found" warning by default (#57)
Browse files Browse the repository at this point in the history
* Disable ".env not found" warning by default

* Mention VERBOSE option in readme

* Fix tests for `load_dotenv` with new verbose option
  • Loading branch information
maxkoryukov authored and theskumar committed Aug 23, 2017
1 parent 5e839d4 commit 7ab249b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ab249b

Please sign in to comment.